Skip to content

feat: add apify::js, the JS semantics shared by ports of JS Actors - #39

Merged
metalwarrior665 merged 1 commit into
masterfrom
feat/js-semantics
Nov 15, 2026
Merged

metalwarrior665 merged 1 commit into
masterfrom
feat/js-semantics

Conversation

@metalwarrior665

Copy link
Copy Markdown
Member

Rust ports of JS Actors have to produce exactly the items the JS Actor produces, so they depend on JS semantics. Until now these helpers lived only in instagram-rs. google-maps-rs, the next port, needs the same ones, so this PR moves them into the SDK instead of having a second copy.

Moved from instagram-rs, with their APIs, doc comments and unit tests:

  • apify::js: Obj (undefined vs null, spreads, key order), at/index/path (?.), defined, truthy, or/or_ref/truthy_or_undefined (||), coalesce (??), strict_equals, to_js_string (String(value)), to_number (+value), unix_secs_to_iso, is_js_space/js_trim, text_of, array_index/js_key_order (Object.keys order)
  • apify::js::json: Json, a JSON value that borrows its strings from the body, with the same helpers
  • apify::js::typed: Js, Raw, List, Key, Skip, FromMap, IntoOwned, read, read_owned, parse_owned and the js_object! macro. The macro is now #[macro_export]ed (apify::js_object! and apify::js::typed::js_object!), and it reaches serde through $crate, so a crate that uses it does not need to depend on serde itself. tests/js.rs and a doc test use it from outside the crate.
  • apify::js::number: js_number_to_string (String(number)), js_number, js_string_to_number (Number(text)), js_parse_float, js_parse_int

New or newly public: math_round (Math.round: a half rounds up toward +∞, and the result is -0 for values in [-0.5, 0)). js_to_fixed (Number(x.toFixed(n))) was the charging manager's private helper; it is now public and charging uses it from here. encodeURIComponent is re-exported from crawlee-rs. The new tests put the JS expression next to each expected value.

Small fixes to the moved code: to_js_string on a float now goes through js_number_to_string, so exponent forms come out as JS writes them (1e+21, 1e-7). to_number on a string now goes through Number(text) semantics: JS white space, 0x/0o/0b, Infinity, and NaN for inf.

Instagram-specific helpers stay in instagram-rs (dedup by property, test-data hooks, truncate). instagram-rs will switch to apify::js in a separate PR by bumping its SDK revision, deleting its own copies and changing the imports to crate::js::… → apify::js::… and crate::utils::common::js_number* → apify::js::number::….


Generated by Claude Code

Moves the JS-semantics helpers of instagram-rs into the SDK, so that the
Rust ports of JS Actors (instagram-rs, google-maps-rs) share them instead
of copying them: `Obj` and `undefined`, truthiness, `??` and `||`,
`String(value)`, `Object.keys` order, `Json`, the typed reading of JSON
(`js_object!`, exported for use from other crates) and the number helpers
(`String(number)`, `Number(text)`, `parseFloat`, `parseInt`).

Adds `Math.round` and makes `Number(x.toFixed(n))` (the charging
manager's) public; `encodeURIComponent` is re-exported from crawlee-rs.
`String(value)` and `+value` of the moved code now use the JS number
conversions (exponent forms, JS white space, hex and `Infinity`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0148RYR4BTGyCYHENSRZjC3S
@metalwarrior665
metalwarrior665 merged commit d0e74c1 into master Nov 15, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants