パッケージの詳細

lutils

nfour13.1k2.4.0

A few reliable utils.

merge, extend, clone, type

readme

 

lutils

TypesSript documented

  • merge for deep merging of objects
  • clone for deep cloning of objects & arrays
  • typeOf for consistant type checking
import { typeOf, merge, clone } from 'lutils'

merge

Merge objects together, traversing objects & arrays recursively

  • merge(subject, ...sources[]) => subject
  • Default depth: 10
import { merge } from 'lutils'

merge({ aa: { cc: 1 }, }, { aa: { cc: 2 } }, { bb: 3 })
=== { aa: { cc: 2 }, bb: 3 }

Construct & configure your own Merge instance

  • new Merge(config).merge
  • See: config
import { Merge } from 'lutils'

const merge = new Merge({ depth: Infinity }).merge

merge(megaDeep, ultraDeep)

Merge, but with two common behaviours, whitelisting and blacklisting

  • merge.white(subject, ...sources[]) => subject
  • merge.black(subject, ...sources[]) => subject
import { merge } from 'lutils'

merge.white({ aa: { bb: 1, cc: 1 } }, { aa: { xx: 2, cc: 2 } })
=== { aa: { bb: 1, cc: 2 } }

merge.black({ aa: { bb: 1, cc: 1 } }, { aa: { xx: 2, cc: 2 } })
=== { aa: { bb: 1, cc: 1, xx: 2 } }

clone

Clones objects & arrays recursively

  • clone(subject) => clonedSubject
  • Default depth: 10
import { clone } from 'lutils'

const cloned = clone({ my: { little: { foo: 'bar' } } })
  • new Clone(config).clone
  • See: config
import { Clone } from 'lutils'

const clone = new Clone({ depth: Infinity }).clone

const cloned = clone({ my: { little: { foo: 'bar' } } })

typeOf

Gets the type of a value as a lowercase string. \ Like the built-in typeof, but works for all primitives.

  • typeOf(value) => string
import { typeOf } from 'lutils'

typeOf(null)
=== 'null'

typeOf(NaN)
=== 'nan'

typeOf([])
=== 'array'

Specific type checkers are also exported and attached to typeOf \ These checkers also supply typescript with type information, meaning they can act a type guards.

  • isBoolean(value) => boolean
  • is<type>(value) => boolean
  • See: ITypeOf
import { typeOf, isBoolean, isString } from 'lutils'

typeOf.isNull(null)
=== true

isString(undefined)
=== false

typeOf.isString('')
=== true

isBoolean(false)
=== true

// Type guarding...

function blah (aa: number|string) {
  if (isString(aa)) {
    // string
    aa += '!!!!'
  } else {
    // number
    ++aa 
  }

  return aa
}

更新履歴

2.3.0 May 17th 2017

Features:

  • [x] new Merge.White().merge will construct a merge.white instance
  • [x] new Merge.Black().merge will construct a merge.black instance
  • [x] Both are also exported as { MergeWhite, MergeBlack }

2.1.1 May 12th 2017

Breaking changes:

  • [x] No more seperate npm modules for each function, only lutils
  • [x] typeOf.Boolean etc. are renamed to isBoolean and exported individually
  • [x] merge api simplified to only merge(subject, ...sources[])
  • [x] clone api simplified to only clone(subject)

New features:

  • [x] Full rewrite in Typescript, providing intellisense
  • [x] Performance increases:
      - `typeOf` performance improved by **~220%**!!!
      - `merge` performance improved by **~30%**!
      - `clone` performance on par.
  • [x] typeOf functions can act as type guards in typescript & Flow
  • [x] Merge provides a constructor for configuration
  • [x] Clone provides a constructor for configuration
  • [x] Merge and Clone have added warnings:
      - When default `depth` is hit
      - When invalid parameters are supplied

1.2.5 Jan 10th 2017

  • Adding typings

1.2.0 August 7th 2016

  • Version bumps

1.0.1 April 23rd 2016

  • Tidy up
  • Constrain versions

1.0.0

  • Rebuilt lutils, splitting each function off
    • lutils-typeof
    • lutils-merge
    • lutils-clone
    • lutils simply exposes all three
  • Converted to ES5 from CoffeeScript
  • Refactored API to be more flexible and consitant with similar utilities, such as underscore
    • merge, merge.whtie, merge.black
      • merge(obj1, obj2, obj3, ...)
      • merge(obj1, obj2, obj3, ..., function() {})
      • merge([obj1, obj2, obj3, ...], options)
    • merge now supports test functions for custom merging
    • clone
      • clone(obj, options)
    • typeOf reamins the same
    • See each modules readme for precise api

0.2.10

  • Fixed depth being compared one unit too low.

0.2.7

  • Fixed an {}.__proto__ mutation bug. clone is now significantly faster.