Skip to content
/ nukit Public

Tiny collection of essential JavaScript utilities

License

Notifications You must be signed in to change notification settings

icmx/nukit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
    • and — alias
  • neither — returns true if at least one of predicates is true
    • or — alias
  • toUnique — creates a predicate function for standard Array.prototype.filter to filter unique values

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