What is Chek? Well we have a guy we call Chekov, as in "I can do zat Captain I can do zat". So we shortened it to "Chek". Hey there's a logger named after "Winston" right.
Anyway Chek is a slimmed down lib for common tasks like check if "is" a type or convert to a type. There are helpers for dealing with strings, converting objects to arrays and back (handy for Firebase type apis) and so on.
Nothing special just a nice little toolkit preventing the need for large libs like Lodash or Underscore (both great of course).
The end game is simple. Have about 90% or so coverage for common tasks preventing the need for a larger footprint.
Comes with 100% test coverage out of the gate. Some handy methods like push, splice, shift, unshfit and so on that handle immutability as well as other methods where applicable.
Installation
npm install chek -s
Usage
Using Typescript
import * as ck from 'chek'; // or import { isString, slugify } from 'chek';
let slug = 'i can Do zAT';
if (ck.isString(slug))
slug = ck.slugify(slug) // result: 'i-can-do-zat';
Using ES5
const ck = require('chek');
// same as above.
Methods
Methods are broken into several categories. Some have only a method or two and we'll likely expand a little on some. Have any suggestions be sure to post an issue we're all ears. Not prideful help out we all win!
- array - things like contains, containsAny, duplicates.
- from - things like fromEpoch, fromJSON, tryWrap & tryRequire.
- functions - noop and noopIf
- is - things like if isString, isBoolean, isFunction.
- object - things like get, set, clone, extend.
- string - things like lowercase, uppercase, slugify, padding, uuid.
- to - handles converting to a type like toBoolean, toRegExp.
- type - handles getting and casting types.
Please Note the following tables are here for convenience you should ALWAYS reference the "docs" below for updated method signatues, there's only so much time in the day.
Array
Method | Params | Returns | Description |
---|---|---|---|
contains | arr: any[], val: any | boolean | Tests if array contains value. |
duplicates | arr: any[], val: any, breakable?: boolean | number | Counts duplicates in array. |
contains | arr: any[], compare: any[] | boolean | Tests if array contains any value. |
keys | obj: {} | string[] | Returns an array of key names within an object. |
flatten | ...arr: any[] | any[] | Flattens nested arrays in immutable way. |
first | arr: any[] | any | Returns first value in array without mutating. |
last | arr: any[] | any | Returns last value in array without mutating. |
orderBy | arr: any[], ...fields: IComparatorField[] | T[] | Orders array of objects by property name, falls back to .sort() for convenience. |
pop | arr: any[] | IArrayResult | Pops value returns object w/o mutating w/ new array and val. |
push | arr: any[], ...args: any[] | IArrayResult | Pushes value without mutating returning new array and val. |
shift | arr: any[] | IArrayResult | Shifts value without mutating returning new array and val. |
splice | arr: any[], start?: number, remove?: number, ...items: any[] | IArrayResult | Splices value without mutating returning new array and val and any inserted items. |
unshift | arr: any[], ...items: any[] | IArrayResult | Unshifts inserting value returning new array and val. |
From
Method | Params | Returns | Description |
---|---|---|---|
fromEpoch | val: number, def?: Date | Date | Creates date from epoch. |
fromJSON | val: string, def? T | T | Safely parses JSON. |
Function
Method | Params | Returns | Description |
---|---|---|---|
noop | n/a | void | Non operation function. |
noopIf | fn?: Function | Function | Returns non-operation function or provided function. |
tryWrap | fn: Function, ...args: any[] | Function | Returns safely wrapped function. |
tryRequire | name: string, def?: any | any | Safely requires node module. |
tryRootRequire | name: string, def?: any | any | Safely requires Root node module. |
Is
Method | Params | Returns | Description |
---|---|---|---|
isArray | val: any | boolean | Checks if is array. |
isBoolean | val: any | boolean | Checks if is boolean. |
isBrowser | override?: string | boolean | Checks if is browser. |
isDate | val: any | boolean | Checks if is date. |
isDebug | debugging?: boolean | boolean | Checks if is node debug mode. |
isEmpty | val: any, comp: any, loose?: boolean | boolean | Checks if is empty. |
isEqual | val: any | boolean | Checks if is equal. |
isError | val: any, prop?: string | boolean | Checks if is an error. |
isFloat | val: any | boolean | Checks if is a flot. |
isFunction | val: any | boolean | Checks if is a function. |
isInfinite | val: any | boolean | Checks if is infinite. |
isInspect | inspecting?: boolean | boolean | Checks if is stated with --inspect or --inspect-brk. |
isInteger | val: any | boolean | Checks if is an integer. |
isNode | n/a | boolean | Checks if is running in node. |
isNumber | val: any | boolean | Checks if is a number. |
isMoment | val: any | boolean | Checks if is is a moment. |
isObject | val: any | boolean | Checks if is an object. |
isPlainObject | val: any | boolean | Checks if is an object literal. |
isPromise | val: any, name?: string | boolean | Checks if is a Promise. |
isRegExp | val: any | boolean | Checks if is a Regular Expression. |
isString | val: any | boolean | Checks if is a string. |
isSymbol | val: any | boolean | Checks if is a Symbol. |
isTruthy | val: any | boolean | Checks if value is truthy. |
isType | val: any, Type: any | boolean | Checks if is of specific class type. |
isUndefined | val: any | boolean | Checks if is undefined. |
isUnique | arr: any[], value: any | boolean | Checks if is unique value in array. |
isValue | val: any | boolean | Checks if is not null and is defined. |
Object
Method | Params | Returns | Description |
---|---|---|---|
clone | obj: any, shallow?: boolean | T | Clones an object with shallow option. |
del | obj: any, key: string | string[], immutable?: boolean | T | Removes properties from object using dot notation optional immutable result. |
get | obj: any, key: string | T | Gets properties from object using dot notation. |
extend | obj: any, ...args: any[] | T | Extends objects pass true as first arg for shallow extend. |
has | obj: any, key: string | boolean | Checks if object has property path. |
omit | obj: any, props: any | any[], immutable?: boolean | T | Omits values from string or arrays or omits from object by property name. |
reverse | obj: any | T | Reverses an object { error: 0 } becomes { 0: 'error' }. |
pick | obj: any, props: string | string[] | T | Picks properties from object by property name. |
put | obj: any, key: string | string[], val: any, immutable?: boolean | T | Pushes value to a property if not an array converts to array. |
set | obj: any, key: string | string[], val: any, immutable?: boolean | T | Sets object value using dot notation optional immutable result. |
String
Method | Params | Returns | Description |
---|---|---|---|
camelcase | val: string | string | Converts string to camelCase. |
capitalize | val: string | string | Converts string to Capitalized. |
decamelcase | val: string, separator?: string | string | Decamelizes a string to option/flag style with dashes. |
lowercase | val: string | string | Converts string to lowercase. |
padLeft | val: string, len: number, offset?: number | string, char?: string | string | Pads a string to the left. |
padRight | val: string, len: number, offset?: number | string, char?: string | string | Pads a string to the right. |
padValues | arr: string[], strategy?: string, offset?: number | string, char?: string | string[] | Pads and array of strings to the widest value. |
split | val: string | string[], chars?: string | string[] | string[] | Splits a string by provided char to scans for known chars. |
slugify | val: string | string | Slugifies a string. |
titlecase | val: string, conjunctions?: boolean | string | Converts a string to Title Case. |
uppercase | val: string | string | Converts a string to UPPERCASE. |
uuid | n/a | string | Creates a uuid using "performance" if available. |
To
Method | Params | Returns | Description |
---|---|---|---|
toArray | val: any, id?: string | T[], def?: T[] | T[] | Convers to an array. |
toBoolean | val: any, def?: boolean | boolean | Converts to boolean. |
toDate | val: any, def?: Date | Date | Converts to Date. |
toDefault | val: any, def?: any | any | Converts to default value if null. |
toEpoch | val: Date, def?: number | number | Converts to an Epoch. |
toFloat | val: Date, def?: number | number | Converts to a float (number w/ decimal). |
toJSON | obj: any, pretty?: number | boolean | string, def?: string | string | Converts to JSON safely. |
toInteger | val: any, def?: number | number | Converts to an integer (a whole number). |
toMap | val: any, id?: string | IMap, def?: IMap | T | Converts to arrays and srings to an object map (see examples in method). |
toNested | val: IMap, def?: IMap | T | Converts object from "toUnnested" to a nested object. |
toNumber | val: any, def?: number | number | Converts to a number. |
toRegExp | val: any, def?: RegExp | RegExp | Converts to a Regular Expression. |
toString | val: any, def?: string | string | Converts to a string. |
toUnnested | obj: IMap, prefix?: boolean | IMap, def?: IMap | T | Converts an object to one flattened level using dot notation keys. |
toWindow | key: any, val?: any | void | Adds methods to window used internally. |
Type
Method | Params | Returns | Description |
---|---|---|---|
castType | val: any, type: any, def?: any | T | Casts a type to specified type. |
getType | val: any, strict?: boolean | string, def?: string | T | Detects the type of the provided value. |
Docs
See https://origin1tech.github.io/chek/
Changes
See CHANGE.md
License
See LICENSE