@profiscience/knockout-contrib-utils-from-js
v2.0.4
Published
[![Version][npm-version-shield]][npm] [![Dependency Status][david-dm-shield]][david-dm] [![Peer Dependency Status][david-dm-peer-shield]][david-dm-peer] [![Dev Dependency Status][david-dm-dev-shield]][david-dm-dev] [![Downloads][npm-stats-shield]][npm-sta
Downloads
80
Readme
utils.fromJS
This package is intended for consumption via the [@profiscience/knockout-contrib] metapackage
Usage
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
$ ./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)