Every project should kickstart with some basic utils :)
npm install kickstart-utilsEvery project should kickstart with some basic utils :)
From small projects to big, all of these utils are useful. with no dependencies!
Includes utils such as isEmpty, isObject, random, match, toDictionary and more.
PRs are more than welcome!
1. Only utils and types that every project would use
2. Which means, KISS (Keep It Simple, Stupid)
3. No dependencies
``shell`
npm install kickstart-utils
- Utils
- cn
- isEmpty
- match
- noop
- random
- range
- run
- stopEventPropagation
- toArray
- toDictionary
- Type check
- isArray
- isBoolean
- isError
- isFunction
- isInteger
- isNullish
- isNumber
- isObject
- isString
- Types
- Nullish
- Primitive
Stands for class-names - returns a string removing anything unrelated to css class names.
`typescript jsx
import {cn} from 'kickstart-utils';
$3
Check if falsy (except 0) or empty object
`js
import { isEmpty } from 'kickstart-utils';isEmpty(null) // true
isEmpty(" ") // true
isEmpty([]) // true
isEmpty({}) // true
`$3
Instead of cumbersome switch case, match for cleaner, reusable code
`js
import { match } from 'kickstart-utils';const value = match("foo" , {
foo: 2,
bar: 3,
default: 5
}) // 2
`$3
For any time we need a noop function
`jsx
import { noop } from 'kickstart-utils';// noop = () => {}
`$3
Random, or random between two numbers
`js
import { random } from 'kickstart-utils'random(4, 8) // 4, 5, 6, 7, 8
`$3
Creates a simple range from 0 to n
For more "complex" range, you can either use this range and map on it or create your own range
`js
import { range } from 'kickstart-utils'range(0) // []
range(5) // [0, 1, 2, 3, 4]
const fromToRange = (from, to) => range(to - from).map(i => i + from)
// There are some edge cases though, and the library should be as lean as possible
// so we keep it simple
const fromToRangeWithStep = (from, to, step) => range(to - from).reduce(
(acc, i) => i < (to - from) / step ? [...acc, i * step + from] : acc, []
)
`$3
For the times we're not sure if the value is a function. For most cases, if we are sure the value is either a function
or undefined, we can use
myFunc?.(args).`js
import { run } from 'kickstart-utils';const myFunc = (text) => 'hi' + text
run(myFunc, 'foo') // 'hi foo'
run('Not a function') // 'Not a function'
`$3
Instead of event => event.stopPropagation()
`jsx
import { stopEventPropagation } from 'kickstart-utils';
`$3
Convert anything to an array
`js
import { toArray } from 'kickstart-utils';toArray(null) // [null]
toArray('hello') // ['hello']
toArray([1, 2, 3]) // [1, 2, 3]
`$3
Convert array of objects to a dictionary
`js
import { toDictionary } from 'kickstart-utils';const arr = [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }]
toDictionary(arr, 'id') // { 1: { id: 1, name: 'foo' }, 2: { id: 2, name: 'bar' } }
`$3
Most of them are self-explanatory.
$3
`js
import { isArray } from 'kickstart-utils';isArray([]) // true
`
$3
`js
import { isBoolean } from 'kickstart-utils';isBoolean("true") // false
isBoolean(true) // true
`
$3
`js
import { isError } from 'kickstart-utils';isError({message: "Error"}) // false
isError(Error()) // true
`
$3
`js
import { isFunction } from 'kickstart-utils';isFunction(() => {}) // true
`
$3
With an option to check on a string.`js
import { isInteger } from 'kickstart-utils';isInteger(3.2) // false
isInteger("12", true) // true
`
$3
Nullish is a type that is either null or undefined.$3
With an option to check on a string.`js
import { isNumber } from 'kickstart-utils';isNumber(123.456) // true
isNumber(".123", true) // true
`
$3
Check if is an object (not Array, or null)
`js
import { isObject } from 'kickstart-utils';isObject([{ foo: 'bar' }]) // false
`$3
`js
import { isString } from 'kickstart-utils';isString("") // true
`$3
Some common ts types
$3
`js
import { Nullish } from 'kickstart-utils';// Nullish = null | undefined
`$3
js primitive types (no
symbol)`js
import { Primitive } from 'kickstart-utils';// Primitive = string | number | bigint | boolean | undefined | null
``