Coder Social home page Coder Social logo

chess.ts's Introduction

chess.ts

A direct port of python-chess to TypeScript!

Install

npm i @jacksonthall22/chess.ts

Contributing

I have compiled some notes about how I am approaching this transpilation effort here. Probably this would be a good starting point.

Ideally, this library will transpile everything from python-chess core copy over to the corresponding files in chess. However, I think the most important and immediate objectives to make this library useful are (in order):

  • Transpile __init__.pyindex.ts
  • Transpile pgn.pypgn.ts
  • Transpile engine.pyengine.ts (WIP: minimal functionality for pgn.ts to work)
  • Create a testing suite

If anyone wants to help contribute (thank you, by the way!), I would recommend starting with the next item in this list. Specifically, engine.py might be a behemoth—threading stuff is not my forte.

Transpilation helper

I noticed that 90% of the work is pretty repetitive when converting Python to TypeScript: switching the class/method/docstring formatting, putting parentheses around if statement conditions, changing Trues to trues, Nones to nulls, and ==s to ===s, etc. I created transpilation_helper.py for this reason to quickly transpile a block of Python code of arbitrary length to TypeScript. This can be used as a decent starting point, but the almost-TS-code it outputs will almost always need additional manual work. To point out just a couple of examples, it does not detect which vars need let or const (and so it omits those keywords completely), and it does not modify if/while conditions (these will need to be manually edited to guarantee the same functionality). Also, it handles methods, but not top-level functions.

To use it, just run python transpilation_helper.py. Copy a block of Python code to the clipboard (triple-click and drag to select multiple whole lines, including the leading indents). Press Enter. The transpiled code will be copied to the clipboard. Paste it into a TypeScript file, continue to make edits, and verify it works the same way as the original Python code.

You might need pip install chess pyperclip.

chess.ts's GPT

Also check out the GPT I made for this project, python-chess to chess.ts helper!. I have provided it with instructions specific to this task, which closely follow my notes in py-to-ts-tips.md (it may even address you as Jackson lol). Thus far it has been an indispensible tool to speed up the manual transpilation effort. It is near perfect at transpiling arbitrarily long functions/methods that do not have complex Python-specific code patterns. However, for example, it struggled with transpiling BaseBoard.copy() in __init__.py, which starts by instantiating a new board with the same dynamic type as self:

board = type(self)(None)

After initially giving a poor response that raised TS errors, I had to probe it for a bit before it came up with a successful idea:

const board = new (this.constructor as new () => this)();

So while it is definitely subject to basic mistakes, it usually gets 90% of the way there. The other 10% usually just takes a bit more artful prompting. Always make sure to verify its solutions with the original Python code on a split screen.

chess.ts's People

Contributors

jacksonthall22 avatar

Stargazers

 avatar

Watchers

 avatar

chess.ts's Issues

Issues in `npm` environment

Example:

> import chess, { Board, Move } from "@jacksonthall22/chess.ts"
undefined
> new Board()
C:\...\node_modules\@jacksonthall22\chess.ts\index.ts:13
import {
^^^^^^

Uncaught:
SyntaxError: Cannot use import statement inside the Node.js REPL, alternatively use dynamic import:

I'm not super familiar with uploading to npm or with TS in general so if someone knows the solution to this, please let me know. I'm guessing I have to manually build to JS before uploading to npm or something along those lines. Will try to solve this soon.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.