Options
All
  • Public
  • Public/Protected
  • All
Menu

Chek

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

MethodParamsReturnsDescription
containsarr: any[], val: anybooleanTests if array contains value.
duplicatesarr: any[], val: any, breakable?: booleannumberCounts duplicates in array.
containsarr: any[], compare: any[]booleanTests if array contains any value.
keysobj: {}string[]Returns an array of key names within an object.
flatten...arr: any[]any[]Flattens nested arrays in immutable way.
firstarr: any[]anyReturns first value in array without mutating.
lastarr: any[]anyReturns last value in array without mutating.
orderByarr: any[], ...fields: IComparatorField[]T[]Orders array of objects by property name, falls back to .sort() for convenience.
poparr: any[]IArrayResultPops value returns object w/o mutating w/ new array and val.
pusharr: any[], ...args: any[]IArrayResultPushes value without mutating returning new array and val.
shiftarr: any[]IArrayResultShifts value without mutating returning new array and val.
splicearr: any[], start?: number, remove?: number, ...items: any[]IArrayResultSplices value without mutating returning new array and val and any inserted items.
unshiftarr: any[], ...items: any[]IArrayResultUnshifts inserting value returning new array and val.

From

MethodParamsReturnsDescription
fromEpochval: number, def?: DateDateCreates date from epoch.
fromJSONval: string, def? TTSafely parses JSON.

Function

CHEK FUNCTION
MethodParamsReturnsDescription
noopn/avoidNon operation function.
noopIffn?: FunctionFunctionReturns non-operation function or provided function.
tryWrapfn: Function, ...args: any[]FunctionReturns safely wrapped function.
tryRequirename: string, def?: anyanySafely requires node module.
tryRootRequirename: string, def?: anyanySafely requires Root node module.

Is

CHEK IS
MethodParamsReturnsDescription
isArrayval: anybooleanChecks if is array.
isBooleanval: anybooleanChecks if is boolean.
isBrowseroverride?: stringbooleanChecks if is browser.
isDateval: anybooleanChecks if is date.
isDebugdebugging?: booleanbooleanChecks if is node debug mode.
isEmptyval: any, comp: any, loose?: booleanbooleanChecks if is empty.
isEqualval: anybooleanChecks if is equal.
isErrorval: any, prop?: stringbooleanChecks if is an error.
isFloatval: anybooleanChecks if is a flot.
isFunctionval: anybooleanChecks if is a function.
isInfiniteval: anybooleanChecks if is infinite.
isInspectinspecting?: booleanbooleanChecks if is stated with --inspect or --inspect-brk.
isIntegerval: anybooleanChecks if is an integer.
isNoden/abooleanChecks if is running in node.
isNumberval: anybooleanChecks if is a number.
isMomentval: anybooleanChecks if is is a moment.
isObjectval: anybooleanChecks if is an object.
isPlainObjectval: anybooleanChecks if is an object literal.
isPromiseval: any, name?: stringbooleanChecks if is a Promise.
isRegExpval: anybooleanChecks if is a Regular Expression.
isStringval: anybooleanChecks if is a string.
isSymbolval: anybooleanChecks if is a Symbol.
isTruthyval: anybooleanChecks if value is truthy.
isTypeval: any, Type: anybooleanChecks if is of specific class type.
isUndefinedval: anybooleanChecks if is undefined.
isUniquearr: any[], value: anybooleanChecks if is unique value in array.
isValueval: anybooleanChecks if is not null and is defined.

Object

CHEK OBJECT
MethodParamsReturnsDescription
cloneobj: any, shallow?: booleanTClones an object with shallow option.
delobj: any, key: string | string[], immutable?: booleanTRemoves properties from object using dot notation optional immutable result.
getobj: any, key: stringTGets properties from object using dot notation.
extendobj: any, ...args: any[]TExtends objects pass true as first arg for shallow extend.
hasobj: any, key: stringbooleanChecks if object has property path.
omitobj: any, props: any | any[], immutable?: booleanTOmits values from string or arrays or omits from object by property name.
reverseobj: anyTReverses an object { error: 0 } becomes { 0: 'error' }.
pickobj: any, props: string | string[]TPicks properties from object by property name.
putobj: any, key: string | string[], val: any, immutable?: booleanTPushes value to a property if not an array converts to array.
setobj: any, key: string | string[], val: any, immutable?: booleanTSets object value using dot notation optional immutable result.

String

CHEK STRING
MethodParamsReturnsDescription
camelcaseval: stringstringConverts string to camelCase.
capitalizeval: stringstringConverts string to Capitalized.
decamelcaseval: string, separator?: stringstringDecamelizes a string to option/flag style with dashes.
lowercaseval: stringstringConverts string to lowercase.
padLeftval: string, len: number, offset?: number | string, char?: stringstringPads a string to the left.
padRightval: string, len: number, offset?: number | string, char?: stringstringPads a string to the right.
padValuesarr: string[], strategy?: string, offset?: number | string, char?: stringstring[]Pads and array of strings to the widest value.
splitval: string | string[], chars?: string | string[]string[]Splits a string by provided char to scans for known chars.
slugifyval: stringstringSlugifies a string.
titlecaseval: string, conjunctions?: booleanstringConverts a string to Title Case.
uppercaseval: stringstringConverts a string to UPPERCASE.
uuidn/astringCreates a uuid using "performance" if available.

To

CHEK TO
MethodParamsReturnsDescription
toArrayval: any, id?: string | T[], def?: T[]T[]Convers to an array.
toBooleanval: any, def?: booleanbooleanConverts to boolean.
toDateval: any, def?: DateDateConverts to Date.
toDefaultval: any, def?: anyanyConverts to default value if null.
toEpochval: Date, def?: numbernumberConverts to an Epoch.
toFloatval: Date, def?: numbernumberConverts to a float (number w/ decimal).
toJSONobj: any, pretty?: number | boolean | string, def?: stringstringConverts to JSON safely.
toIntegerval: any, def?: numbernumberConverts to an integer (a whole number).
toMapval: any, id?: string | IMap, def?: IMapTConverts to arrays and srings to an object map (see examples in method).
toNestedval: IMap, def?: IMapTConverts object from "toUnnested" to a nested object.
toNumberval: any, def?: numbernumberConverts to a number.
toRegExpval: any, def?: RegExpRegExpConverts to a Regular Expression.
toStringval: any, def?: stringstringConverts to a string.
toUnnestedobj: IMap, prefix?: boolean | IMap, def?: IMapTConverts an object to one flattened level using dot notation keys.
toWindowkey: any, val?: anyvoidAdds methods to window used internally.

Type

CHEK TYPE
MethodParamsReturnsDescription
castTypeval: any, type: any, def?: anyTCasts a type to specified type.
getTypeval: any, strict?: boolean | string, def?: stringTDetects the type of the provided value.

Docs

See https://origin1tech.github.io/chek/

Changes

See CHANGE.md

License

See LICENSE

Generated using TypeDoc