1.7.2 • Published 5 months ago

nukit v1.7.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

nukit

Tiny collection of essential JavaScript utilities.

Usage

Install:

npm i nukit

Import:

import { isNumber, oneOf, pick } from 'nukit';

if (oneOf([null, 'foo', 42], isNumber)) {
  console.log('There is a number!');
}

const value = pick(['apple', 'banana', 'carrot']);

Why?

  • TypeScript: nice types support
  • Small & Simple: 37 essential functions in about 5 KBs of code
  • Modern: doesn't rewrites ESNext features that already exists

Contents

Filters

  • isUndefined — returns true if value is undefined
  • isNull — returns true if value is null
  • isBoolean — returns true if value is a boolean
  • isNumber — returns true if value is a number
  • isString — returns true if value is a string
  • isPrimitive — returns true if value is a primitive, i.e. undefined, null, boolean, number or string
  • isArray — returns true if value is array of any items
  • isObject — returns true if value is an object but not null nor array
  • isEmpty — returns true if value is empty, i.e. undefined, null, empty string, empty array or empty object (i.e. object without own properties)
  • allOf — returns true if all values in array meets filter condition
  • oneOf — returns true if at least one of values in array meets filter condition
  • equally — returns true if all of predicates are true
  • neither — returns true if at least one of predicates is true
    • or — alias
  • unique — returns an array without duplicates
    • See also: byUnique, a predicate version for array chaining

Mappers

  • toBoolean — maps value into a boolean if possible
  • toNumber — maps value into a number if possible
  • toString — maps value into a string
  • filterObject — filters object entries by predicate like standard array's filter
  • mapObject — maps object entries by callback function like standard array's map
  • repeat — repeats some action for a specified number of times
  • byKeys — creates a comparator function for standard array's sort method to sort elements by keys provided in a custom order

Random

  • bool — returns random boolean value
  • int — returns random integer number value
  • float — returns random float number value
  • char — returns random character value
  • str — returns random string value
  • pick — returns random value from an array of one or more items
  • shuffle — returns shuffled copy of array
  • weighted — returns weighted random value from an array of values and weights

Seedable Random

Just like random utilities above, but with seeding support.

  • seedBool — creates seeded version of bool function
  • seedInt — creates seeded version of char function
  • seedFloat — creates seeded version of float function
  • seedChar — creates seeded version of char function
  • seedStr — creates seeded version of str function
  • seedPick — creates seeded version of pick function
  • seedShuffle — creates seeded version of shuffle function
  • seedWeighted — creates seeded version of weighted function

Date

  • ts — returns a Unix numeric timestamp for current or specified date
  • isValidDate — returns true if value is valid Date or suits for Date creation
  • diffDate — returns total amount of time between two dates (years, months, days, hours etc)

Error

  • when/drop — returns a function to drop an error when predicate is true

RegExp

  • r — creates a RegExp from a template string, gu flags are enabled
  • re — creates a RegExp from a string in a short way

Development

# run tests
npm run test

# get tests coverage report
npm run coverage

# build a package
npm run build

# publish a release
npm run release
1.7.2

5 months ago

1.7.1

1 year ago

1.7.0

2 years ago

1.6.0

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.0

2 years ago