Giter Club home page Giter Club logo

kickstart-utils's Introduction

Every 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

Installation

npm install kickstart-utils

API

Utils

cn

Stands for class-names - returns a string removing anything unrelated to css class names.

import {cn} from 'kickstart-utils';

<div class={cn('foo', a && 'bar', b, 'baz')}/> // class="foo baz" 

isEmpty

Check if falsy (except 0) or empty object

import { isEmpty } from 'kickstart-utils';

isEmpty(null) // true
isEmpty(" ") // true
isEmpty([]) // true
isEmpty({}) // true

match

Instead of cumbersome switch case, match for cleaner, reusable code

import { match } from 'kickstart-utils';

const value = match("foo" , {
  foo: 2,
  bar: 3,
  default: 5
}) // 2

noop

For any time we need a noop function

import { noop } from 'kickstart-utils';

// noop = () => {}

<div onClick={noop}/> 

random

Random, or random between two numbers

import { random } from 'kickstart-utils'

random(4, 8) // 4, 5, 6, 7, 8

range

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

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, []
)

run

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).

import { run } from 'kickstart-utils';

const myFunc = (text) => 'hi' + text
run(myFunc, 'foo') // 'hi foo'
run('Not a function') // 'Not a function'

stopEventPropagation

Instead of event => event.stopPropagation()

import { stopEventPropagation } from 'kickstart-utils';

<input onChange={stopEventPropagation} />

toArray

Convert anything to an array

import { toArray } from 'kickstart-utils';

toArray(null) // [null]
toArray('hello') // ['hello']
toArray([1, 2, 3]) // [1, 2, 3]

toDictionary

Convert array of objects to a dictionary

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' } }

Type check (with ts type guards)

Most of them are self-explanatory.

isArray

import { isArray } from 'kickstart-utils';

isArray([]) // true

isBoolean

import { isBoolean } from 'kickstart-utils';

isBoolean("true") // false
isBoolean(true) // true

isError

import { isError } from 'kickstart-utils';

isError({message: "Error"}) // false
isError(Error()) // true

isFunction

import { isFunction } from 'kickstart-utils';

isFunction(() => {}) // true

isInteger

With an option to check on a string.

import { isInteger } from 'kickstart-utils';

isInteger(3.2) // false
isInteger("12", true) // true

isNullish

Nullish is a type that is either null or undefined.

isNumber

With an option to check on a string.

import { isNumber } from 'kickstart-utils';

isNumber(123.456) // true
isNumber(".123", true) // true

isObject

Check if is an object (not Array, or null)

import { isObject } from 'kickstart-utils';

isObject([{ foo: 'bar' }]) // false

isString

import { isString } from 'kickstart-utils';

isString("") // true

Types

Some common ts types

Nullish

import { Nullish } from 'kickstart-utils';

// Nullish = null | undefined

Primitive

js primitive types (no symbol)

import { Primitive } from 'kickstart-utils';

// Primitive = string | number | bigint | boolean | undefined | null

kickstart-utils's People

Contributors

chikalaka avatar edan-kodem avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.