:metal: KnockoutJS Goodies Monorepo
This package is intended for consumption via the [@profiscience/knockout-contrib] metapackage
fromJS(src[, mapArrayElements = false])
Creates a tree of observables from src
.
If mapArrayElements
is true, array elements will be passed to fromJS
as well, else bare objects/primitives.
The much needed inverse to the undocumented ko.toJS
function; a dumb version of ko.mapping.fromJS that is a lot faster.
import { fromJS } from '@profiscience/knockout-contrib'
const foos = {
foo: 'foo',
bar: {
baz: 'baz',
qux: ['qux'],
},
}
fromJS(foos)
// {
// foo: ko.observable('foo'),
// bar: {
// baz: ko.observable('baz'),
// qux: ko.observableArray([
// 'qux'
// ])
// }
// }
fromJS(foos, true)
// {
// foo: ko.observable('foo'),
// bar: {
// baz: ko.observable('baz'),
// qux: ko.observableArray([
// ko.observable('qux') <--------- Array contents mapped when second argument is true
// ])
// }
// }
$ ./benchmark.ts
utils.fromJS x 316,313 ops/sec ±3.37% (79 runs sampled)
utils.fromJS (mapArrayElements) x 151,401 ops/sec ±3.24% (81 runs sampled)
mapping.fromJS x 11,995 ops/sec ±3.82% (81 runs sampled)