Repository navigation
feat: add apify::js, the JS semantics shared by ports of JS Actors - #39
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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(undefinedvsnull, 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.keysorder)apify::js::json:Json, a JSON value that borrows its strings from the body, with the same helpersapify::js::typed:Js,Raw,List,Key,Skip,FromMap,IntoOwned,read,read_owned,parse_ownedand thejs_object!macro. The macro is now#[macro_export]ed (apify::js_object!andapify::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.rsand 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_intNew or newly public:
math_round(Math.round: a half rounds up toward +∞, and the result is-0for 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.encodeURIComponentis 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_stringon a float now goes throughjs_number_to_string, so exponent forms come out as JS writes them (1e+21,1e-7).to_numberon a string now goes throughNumber(text)semantics: JS white space,0x/0o/0b,Infinity, andNaNforinf.Instagram-specific helpers stay in instagram-rs (dedup by property, test-data hooks,
truncate). instagram-rs will switch toapify::jsin a separate PR by bumping its SDK revision, deleting its own copies and changing the imports tocrate::js::…→apify::js::…andcrate::utils::common::js_number*→apify::js::number::….Generated by Claude Code