text stringlengths 0 1.99k |
|---|
turn into [ "66" ] using Object.keys(). The fact that our number is now a |
string is luckily not a problem for String.fromCharCode (it will implicitly |
call .valueOf()). |
Chaining this all together allows us to write the following $chr function: |
;; A convoluted way to call String.fromCharCode on a single number. |
(func $chr (param $c i32) (result externref) |
(local $tmp externref) |
;; This is just a way to get an array with one element, |
;; so groupBy invokes the callback just once. |
(call $getOwnPropertyNames (call $values (global.get $prototype))) |
(local.set $tmp) ;; [ 'length' ] |
;; First we call Object.groupBy() on a single-element array, with a |
;; callback that returns a fixed value ($c), to create an object with |
;; just that key. For example, for 66 we'd obtain { "66": ["length"] } |
(local.get $c) |
(global.set $g_val_i) |
(call $groupBy_i (local.get $tmp) (ref.func $return_val_i)) |
;; Then, we call String.fromCharCode on it by passing e.g. [ "66" ] |
;; (the result of Oject.keys()) to Object.groupBy() |
(call $groupBy_e |
(call $keys) |
(global.get $String_constructor_fromCharCode) |
) |
;; Now Object.keys() gives ['A\x00'], so we do _[0][0] to get just 'A' |
(call $keys) |
(i32.const 0) |
(call $array_get_nth_element) |
(i32.const 0) |
(call $array_get_nth_element) |
) |
----[ 5.5 Accumulating into a string |
With individual character-strings now available to us, we need a way to |
concatenate them. The first step is a method of accumulating characters |
into a list. For this, we can use the merge primitive provided by |
Object.assign(): |
> Object.assign({"foo":"bar"}, {"lorem": "ipsum"}) |
{ foo: 'bar', lorem: 'ipsum' } |
We'll assign each character to a unique, incrementing key (property name), |
accumulating them on a single object (though, each value will be wrapped in |
an array): |
;; Adds $value to obj, under a new unique (incrementing) key |
;; This is so we can accumulate values with unique incrementing keys on |
;; an object, used to build an array later. |
(func $add_value_to_obj (param $obj externref) (param $value externref) |
(local $tmp externref) |
;; Create a new object ($tmp) which has just a single property with a |
;; controlled value ($value) and a unique key (the return value of the |
;; callback tells Object.groupBy what property name to use) |
(call $groupBy_i (local.get $value) (ref.func $return_incr_ctr)) |
(local.set $tmp) |
;; Use Object.assign() to add the property to $obj |
(call $assign (local.get $obj) (local.get $tmp)) |
) |
After calling this multiple times, this will result in an object such as: |
{ |
'1': [ 'H' ], |
'2': [ 'e' ], |
'3': [ 'l' ], |
'4': [ 'l' ], |
'5': [ 'o' ] |
} |
Which we then turn into a list with Object.values() |
[ [ 'H' ], [ 'e' ], [ 'l' ], [ 'l' ], [ 'o' ] ] |
How is this useful to us? Well: |
> String.raw({raw:[['H'],['e'],['l'],['l'],['o']]}) |
'Hello' |
This function is normally used under the hood with raw template literals, |
but it is perfect for our use-case. Its argument should be an object |
containing a "raw" property with the array of string-parts to concatenate. |
The fact that each element is wrapped in an array by itself is no problem: |
the implicit .toString() will strip them: |
> ['A'].toString() |
'A' |
Obtaining a reference to String.raw() is done in the same way we obtained |
String.fromCharCode(), and creating the argument object is again possible |
with Object.groupBy() and a custom callback: |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.