pulse.js
Python bindings for JavaScript builtins in transpiled code
pulse.js
The pulse.js module provides typed Python wrappers for JavaScript objects, functions, and built-ins. Use these when writing @ps.javascript decorated functions that get transpiled to JavaScript.
Why pulse.js?
When you write a @ps.javascript function, your Python code gets transpiled to JavaScript. But Python and JavaScript have different built-ins: Python has dict and set, JavaScript has Map and Set. The pulse.js module bridges this gap by providing Python classes that transpile to their JavaScript equivalents.
from pulse.js import Set, Array, Math, console
@ps.javascript
def example():
items = Set([1, 2, 3]) # -> new Set([1, 2, 3])
arr = Array.from_(items) # -> Array.from(items)
rounded = Math.floor(3.7) # -> Math.floor(3.7)
console.log(rounded) # -> console.log(rounded)Module organization
The module provides three categories of exports:
Classes (constructors + static methods)
Use new when called, provide static methods:
- Array - Indexed collection
- Date - Date and time
- Error - Error types
- Map - Key-value collection
- Number - Number utilities
- Object - Object utilities
- Promise - Async operations
- RegExp - Regular expressions
- Set - Unique value collection
- String - String utilities
- WeakMap - Weak key-value collection
- WeakSet - Weak value collection
Namespaces (static functions only)
Access methods via the namespace:
- Math - Mathematical functions
- JSON - JSON parsing/stringifying
- console - Logging
- window - Browser window
- document - DOM document
- navigator - Browser info
React bindings
Hooks and components for React:
- useState, useEffect, useRef, ... - React hooks
- Suspense, lazy - React components
Special functions
throw(error)- Throw an error (statement, not expression)obj(key=value)- Create plain JS object literalundefined- JavaScript undefined value
Import patterns
Import classes and namespaces from pulse.js:
from pulse.js import Set, Map, Array, Math, console, windowOr import namespace modules for aliased access:
import pulse.js.math as Math
import pulse.js.json as JSON
Math.floor(3.7) # -> Math.floor(3.7)
JSON.stringify(data) # -> JSON.stringify(data)Import React hooks from pulse.js.react:
from pulse.js.react import useState, useEffect, useRefSee also
- Builtins reference - Array, Map, Set, Promise, etc.
- Namespaces reference - Math, JSON, console, window, document
- React reference - Hooks and components
- @ps.javascript decorator - Transpilation decorator