0.0.1 • Published 8 months ago

@syncify/utils v0.0.1

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
8 months ago

@syncify/utils

Utilties box designed to simplify common development tasks. Though part of the Syncify project, this package can also function independently. Provides a collection of helper functions to enhance your development workflow and alleviate extranous code operations.

Installation

pnpm add @syncify/utils

Usage

Functions within this package are organized by their operational focus, aiding in various data manipulations, string operations, array handling, and asynchronous tasks.

String Manipulation

import * as u from '@syncify/utils'

// Join a string together with no space
u.glue('foo', 'bar') => 'foobar'

// Join a string together with a single space character
u.glueString('foo', 'bar') => 'foo bar'

// Join string array or spread strings with newline characters
u.glueLines('foo', 'bar') => 'foo\nbar\n'

// Generate a random string UUID
u.uuid() => 'a1b2c3'

// Converts string input to a handle by replacing non-alphanumeric characters with hyphens
u.handleize('hello world') => 'hello-world'

// Adds an 's' to the end of a word if size is not 1
u.plural('example', 1) => 'examples'

// Capitalize the first letter of a string
u.toUpcase('example') => 'Example'

// Append an `st`, `nd`, `rd` or `th` to the end of a number
u.addSuffix(10) => '10th'

Type Checks

import * as u from '@syncify/utils'

// Check whether value is `undefined` or `null`
u.isNil(null) => true

// Check whether value is an even number
u.isEven(1) => false

// Check whether a Buffer or String is empty after trimming
u.isEmptyString('') => true

// Check whether an object or array is empty
u.isEmpty('') => true

// Check if param is an array type
u.isArray([]) => true

// Check if param is an object type
u.isObject({}) => true

// Check if param is a non-empty string type
u.isStringStrict('x') => true

// Check if param is a string type
u.isString('') => true

// Check if param is a date type
u.isDate(Date) => true

// Check if param is a regular expression type
u.isRegex(/[a-z]/) => true

// Check if param is a function type
u.isFunction(() => {}) => true

// Check if param is a boolean type
u.isBoolean('true') => false

// Check if param is a constructor or function prototype
u.isConstructor(class X {}) => true

// Check if param is a number type, hexadecimal, or math-like string
u.isNumberStrict(0x) => true

// Check if param is a number type
u.isNumber(1) => true

// Check if param is `NaN`
u.isNaN(NaN) => true

// Check if param is null type
u.isNull(null) => true

// Check if param is undefined type
u.isUndefined(undefined) => true

// Check if param is an Asynchronous function
u.isAsync(async () => {}) => true

// Check if param is a Buffer type
u.isBuffer(Buffer) => true

Object Structures

import * as u from '@syncify/utils'

//  Check if a deeply nested path exists in an object
u.hasPath("a.b", { a: { b: 'value' } }) => true

// Check if a property exists directly in an object
u.has('name', { name: 'John', age: 30 }) => true

// Return a function to check if a property exists in an object for currying
u.hasProp({ name: 'John', age: 30 })('name') => true

Promise / Event Loop

import * as u from '@syncify/utils'

// Returns a promise that resolves in the next event loop
u.pNext() => Promise

// Runs provided tasks in series, handling both async and sync tasks
u.pSerial([ async () => {} ]) => Promise

// Creates a debounced function for promise-returned functions with error handling
u.debouncePromise(() => Promise, 1, onError: (e) => {}) => (v) => void

Miscellaneous

import * as u from '@syncify/utils'

// Creates an object with null prototype, optionally accepts object param
u.object() => {}

// An immutable merge utility for objects with deep support
u.merge({ a: 'x' }, { b: 1 }) => { a: 'x', b: 1 }

// Returns an MD5 hash from string or buffer
u.checksum('foo') => 'a1b2c3d4e6f8g9a1b2c3d4e6f8g9'

Contributing

This package is designed for usage within Syncify. Contributions are not accepted unless they pertain to the core Syncify module. If contributions fall into that category, fork the Syncify project.

0.0.1

8 months ago