ko-contrib-utils
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

ko-contrib-utils

NPM Version WTFPL Travis Coverage Status Dependency Status Peer Dependency Status

Table of Contents

Utils

defaults(dest, defaultValues[, mapArrays = false])

Creates observables for enumerable string properties of defaultValues where undefined in the destination object.

If mapArrays is true, array elements will be created as mapped observables, else bare objects/primitives.

import { defaults } from 'ko-contrib-utils'

const foos = { foo: 'foo' }
defaults(foos, { foo: 'bar', bar: 'bar' })

foos()
// { foo: 'foo', bar: 'bar' }
fromJS(src[, mapArrays = false])

Creates a tree of observables from src.

If mapArrays is true, array elements will be created as mapped observables, 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 'ko-contrib-utils'

const foos = {
  foo: 'foo',
  bar: {
    baz: 'baz',
    qux: ['qux']
  }
}

fromJS(foos, true)
// {
//   foo: ko.observable('foo'),
//   bar: {
//     baz: ko.observable('baz'),
//     qux: ko.observableArrap([
//       ko.observable('qux')
//     ])
//   }
// }
merge(dest, src[, mapArrays = false])

For each enumerable property of src, a) creates an observable when undefined on dest b) updates when existing observable on dest c) sets when existing non-observable on dest.

If mapArrays is true, array elements will be created as mapped observables, else bare objects/primitives. NOTE: Merging new arrays onto existing ones that have been mapped deep will create new observable elements, not update the existing ones. No attempts are made to key elements, nor will they. If you need more, you probably want ko.mapping which is much more powerful, but far slower.

import { merge } from 'ko-contrib-utils'

const foos = {
  foo: ko.observable('foo'),
  bar: 'bar'
}

merge(foos, {
  foo: 'new foo',
  bar: 'new bar',
  baz: 'baz'
})

foos()
// {
//   foo: ko.observable('new foo'),
//   bar: 'new bar',
//   baz: ko.observable('baz')
// }

Package Sidebar

Install

npm i ko-contrib-utils

Weekly Downloads

0

Version

0.4.0

License

WTFPL

Last publish

Collaborators

  • caseywebb