@kamataryo/extypes

0.2.57 • Public • Published

@kamataryo/extypes

Build Status Build status Code Climate Codecov branch

npm (scoped) downloads

Dependency Status devDependency Status

License: MIT

Light and safe JavaScript Data type extensions.

features

  • collision free with Symbol property.
  • Partial import is Available. This saves file size when bundled

install

$ npm install @kamataryo/extypes --save-dev

usage

// import all
import props from '@kamataryo/extypes'
console.log([0, 1, 2][props.Array.$random]) // 0, 1, or 2

// partial import on a certain type
import props, { $random } from '@kamataryo/extypes/Array'
console.log([0, 1, 2][props.$random]) // 0, 1, or 2
console.log([0, 1, 2][$random]) // 0, 1, or 2

// partial import on a certain property/method
import $radom from '@kamataryo/extypes/Array/$random'
console.log([0, 1, 2][$random]) // 0, 1, or 2

APIs

Object

Object[$dig](props, fallback)

Dig nested properties.

{ a : { b : { c : 'd' } } }[$dig](['a', 'b', 'c']) // 'd'
{ a : { b : { c : 'd' } } }[$dig](['a', 'X'], 'no props') // 'no props'

Object[$keys]

Wrapper of Object.keys.

{ foo: 'FOO', bar: 'BAR' }[$keys] // ['foo', 'bar']

Object[$multiply]()

{ a: 'A' }[$multiply](3) // [{ a: 'A' }, { a: 'A' }, { a: 'A' }]

Object[$values]

Wrapper of Object.values.

{ foo: 'FOO', bar: 'BAR' }[$values] // ['FOO', 'BAR']

Object[$switch]

Wrapper of switz.

'abc'[$switch]
  .case('ABC', () => console.log('not match'))
  .case('abc', () => console.log('match!'))
  .default(() => console.log('not match'))
// 'match!'

Array

Array[$cummulatives]

get cummulative values of the array.

[1, 10, 100][$cummulative] // [1, 11, 111]

Array[$flatten]()

Flatten array.

[[0, 1], [2], 3][$flatten]() // [0, 1, 2, 3]

Array[$push](any)

Non-destructive altenative method for Array.push.

[0, 1, 2][$push](3) // [0, 1, 2, 3]

Array[$random]

Pick an element randomly.

[0, 1, 2][$random] // 0, 1, or 2

Array[$weightedRandom](array)

Pick an element randomly and weightedly.

[0, 1, 2][$weightedRandom]([1, 1, 2]) // 0(25%), 1(25%) or 2(50%)

Function

Function[$partialApply](...args)

Curry the function.

((a, b, c) => a + b + c)[$partialApply](1) // (b + c) => 1 + b + c

Function[$appliedArguments]

Applied argument after curried.

((a, b, c) => a + b + c)[$partialApply](1, 2)[$appliedArguments] // [1, 2]

Function[$repeat](args, num)

Call function repeatedly.

((a, b, c) => a + b + c)[$repeat]([1, 2, 3], 2) // [6, 6]

Number

Number[$toRange]()

Create range array.

100[$toRange]() // [0, 1, ... ,99]

Number[$zeroPadding](int, dec)

Create zero padding string.

123[$zeroPadding](5)         // '00123'
1.23[$zeroPadding](false, 3) // '1.230'
12.3[$zeroPadding](3, 3)     // '012.300'

initialize deploy secrets (for commiters)

Below initializes..

  • encrypted npm deploy token
  • encrypted GitHub deploy key

and send new private key to the concerned remote repository.

$ .bin/travis-setup.sh kamataryo/extypes

deployment (for commiters)

$ npm version patch -m "some patch"
$ npm version minor -m "some minor update"
$ npm version major -m "some major update"

TODOs

  • check performance
  • invalid arguments
  • How to deal with Lodash/Underscore?

Package Sidebar

Install

npm i @kamataryo/extypes

Weekly Downloads

5

Version

0.2.57

License

MIT

Last publish

Collaborators

  • kamataryo