A collection of reusable utilities.
Repository ✦ Package ✦ Releases ✦ Discussions
pnpm add @hypernym/utils
- TypeScript friendly
- Fully tree-shakeable
- No dependencies
Install @hypernym/utils package:
# via pnpm
pnpm add @hypernym/utils# via npm
npm install @hypernym/utilsHere are some examples of how to integrate Hyperutils from a CDN via a script tag.
Also, it is possible to download files manually and serve them accordingly.
<script type="module">
import { isNull, isString, ... } from 'https://unpkg.com/@hypernym/utils/dist/index.min.js'
</script><script src="https://unpkg.com/@hypernym/utils/dist/index.iife.js"></script>
<script>
const { isNull, isString, ... } = Hyperutils
</script><script src="https://unpkg.com/@hypernym/utils/dist/index.umd.js"></script>
<script>
const { isNull, isString, ... } = Hyperutils
</script>After installation, import utils into your project:
// ESM & TS
import { isNull, isString, ... } from '@hypernym/utils'
// ESM & TS
import { exists, copy, ... } from '@hypernym/utils/fs'
// Types
import type { IsAny, RequiredDeep, ... } from '@hypernym/utils'Checks if the code is running in the browser.
import { isBrowser } from '@hypernym/utils'
isBrowser // trueReturns a boolean if the given value is a null.
import { isNull } from '@hypernym/utils'
isNull(null) // trueReturns a boolean if the given value is a undefined.
import { isUndefined } from '@hypernym/utils'
isUndefined(undefined) // trueReturns a boolean if the given value is a string.
import { isString } from '@hypernym/utils'
isString('@hypernym/utils') // trueReturns a boolean if the given value is an empty string.
import { isStringEmpty } from '@hypernym/utils'
isStringEmpty('') // trueReturns a boolean if the given value is a boolean.
import { isBoolean } from '@hypernym/utils'
isBoolean(true) // trueReturns a boolean if the given value is a true.
import { isTrue } from '@hypernym/utils'
isTrue(true) // trueReturns a boolean if the given value is a false.
import { isFalse } from '@hypernym/utils'
isFalse(false) // trueReturns a boolean if the given value is a number.
import { isNumber } from '@hypernym/utils'
isNumber(33) // trueReturns a boolean if the given value is a array.
import { isArray } from '@hypernym/utils'
isArray([]) // trueReturns a boolean if the given value is an empty array.
import { isArrayEmpty } from '@hypernym/utils'
isArrayEmpty([]) // trueReturns a boolean if the given value is a object.
import { isObject } from '@hypernym/utils'
isObject({}) // trueReturns a boolean if the given value is an empty object.
import { isObjectEmpty } from '@hypernym/utils'
isObjectEmpty({}) // trueReturns a boolean if the given value is a Function.
import { isFunction } from '@hypernym/utils'
isFunction(() => {}) // trueReturns a boolean if the given value is a NaN.
import { isNaNValue } from '@hypernym/utils'
isNaNValue(NaN) // trueReturns a boolean if the given value is a RegExp.
import { isRegExp } from '@hypernym/utils'
isRegExp(/^hypernym/) // trueReturns a boolean if the given value is a Map.
import { isMap } from '@hypernym/utils'
isMap(new Map()) // trueReturns a boolean if the given value is a WeakMap.
import { isWeakMap } from '@hypernym/utils'
isWeakMap(new WeakMap()) // trueReturns a boolean if the given value is a Set.
import { isSet } from '@hypernym/utils'
isSet(new Set()) // trueReturns a boolean if the given value is a WeakSet.
import { isWeakSet } from '@hypernym/utils'
isWeakSet(new WeakSet()) // trueReturns a boolean if the given value is a symbol.
import { isSymbol } from '@hypernym/utils'
isSymbol(Symboly('hypernym')) // trueReturns a boolean if the given value is a Date.
import { isDate } from '@hypernym/utils'
isDate(new Date()) // trueReturns a boolean if the given value is a bigint.
import { isBigInt } from '@hypernym/utils'
isBigInt(1n) // trueReturns a boolean if the given value is a Infinity.
import { isInfinity } from '@hypernym/utils'
isInfinity(Infinity) // trueReturns a boolean if the given value is a URL.
import { isURL } from '@hypernym/utils'
isURL(new URL('https://localhost:3000')) // trueReturns a boolean if the given value is a Error.
import { isError } from '@hypernym/utils'
isError(new Error()) // trueReturns a boolean if the given value is a Primitive.
import { isPrimitive } from '@hypernym/utils'
isPrimitive(true) // trueReturns a boolean if the given value is a Element.
import { isElement } from '@hypernym/utils'
isElement(el) // trueReturns a boolean if the given value is a HTMLElement.
import { isHtmlElement } from '@hypernym/utils'
isHtmlElement(htmlEl) // trueReturns a boolean if the given value is a SVGElement.
import { isSvgElement } from '@hypernym/utils'
isSvgElement(svgEl) // trueReturns a boolean if the given value is a NodeList.
import { isNodeList } from '@hypernym/utils'
isNodeList(document.querySelectorAll('div')) // trueReturns a boolean if the given value is an empty NodeList.
import { isNodeListEmpty } from '@hypernym/utils'
isNodeListEmpty(document.querySelectorAll('divs')) // trueReturns a boolean if the given value is a HTMLCollection.
import { isHtmlCollection } from '@hypernym/utils'
isHtmlCollection(document.getElementsByClassName('el')) // trueReturns a boolean if the given value is an empty HTMLCollection.
import { isHtmlCollectionEmpty } from '@hypernym/utils'
isHtmlCollectionEmpty(document.getElementsByClassName('els')) // trueChecks if the file or directory exists.
import { exists } from '@hypernym/utils/fs'
await exists('dir/file.ts') // trueReads the entire contents of a file.
import { read } from '@hypernym/utils/fs'
await read('dir/subdir/file.ts')Reads the contents of a directory recursively.
import { readdir } from '@hypernym/utils/fs'
await readdir('dir/subdir')Writes data to a file recursively.
import { write } from '@hypernym/utils/fs'
await write('dir/subdir/file.ts', `console.log('Hello World!')`)Copies files or directories recursively.
Accepts a single source or a range of sources.
import { copy } from '@hypernym/utils/fs'
await copy('src/subdir/file.ts', './dist/subdir')Creates a directory recursively.
Accepts a single path or a range of paths.
import { mkdir } from '@hypernym/utils/fs'
await mkdir('src/subdir')Removes files and directories recursively.
Accepts a single path or a range of paths.
import { remove } from '@hypernym/utils/fs'
await remove('src/subdir/file.ts')Matches any primitive value.
import type { Primitive } from '@hypernym/utils'
type OnlyPrimitives<T> = T extends Primitive ? T : never
type Filtered = OnlyPrimitives<string | number | {} | Date> // string | numberMatches any Primitive, Date or RegExp value.
import type { BuiltIn } from '@hypernym/utils'
type OnlyBuiltIns<T> = T extends BuiltIn ? T : never
type Filtered = OnlyBuiltIns<string | Date | {} | RegExp> // string | Date | RegExpReturns a boolean if the given type is a null.
import type { IsNull } from '@hypernym/utils'
type A = IsNull<null> // true
type B = IsNull<string> // false
type C = IsNull<undefined> // falseReturns a boolean if the given type is a any.
import type { IsAny } from '@hypernym/utils'
type A = IsAny<any> // true
type B = IsAny<string> // false
type C = IsAny<unknown> // falseReturns a boolean if the given type is a never.
import type { IsNever } from '@hypernym/utils'
type A = IsNever<never> // true
type B = IsNever<number> // false
type C = IsNever<undefined> // falseConstructs a type by recursively setting all properties as optional.
Use Partial<T> for one level.
import type { PartialDeep } from '@hypernym/utils'
type PartialObject = PartialDeep<Object>
// Disables recursive mode for arrays and tuples.
type PartialObject = PartialDeep<Object, { arrays: false }>Constructs a type by recursively setting all properties as required.
Use Required<T> for one level.
import type { RequiredDeep } from '@hypernym/utils'
type RequiredObject = RequiredDeep<Object>
// Disables recursive mode for arrays and tuples.
type RequiredObject = RequiredDeep<Object, { arrays: false }>Developed in ðŸ‡ðŸ‡· Croatia, © Hypernym Studio.
Released under the MIT license.