@hypernym/utils
v3.4.6
Published
A collection of reusable utilities.
Readme
Features
- TypeScript friendly
- Fully tree-shakeable
- No dependencies
Installation
Install @hypernym/utils package:
# via pnpm
pnpm add @hypernym/utils# via npm
npm install @hypernym/utilsCDN
Here 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.
ESM (minified)
<script type="module">
import { isNull, isString, ... } from 'https://unpkg.com/@hypernym/utils/dist/index.min.js'
</script>IIFE (minified)
<script src="https://unpkg.com/@hypernym/utils/dist/index.iife.js"></script>
<script>
const { isNull, isString, ... } = Hyperutils
</script>UMD (minified)
<script src="https://unpkg.com/@hypernym/utils/dist/index.umd.js"></script>
<script>
const { isNull, isString, ... } = Hyperutils
</script>Usage
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'is
isBrowser
Checks if the code is running in the browser.
import { isBrowser } from '@hypernym/utils'
isBrowser // trueisNull
Returns a boolean if the given value is a null.
import { isNull } from '@hypernym/utils'
isNull(null) // trueisUndefined
Returns a boolean if the given value is a undefined.
import { isUndefined } from '@hypernym/utils'
isUndefined(undefined) // trueisString
Returns a boolean if the given value is a string.
import { isString } from '@hypernym/utils'
isString('@hypernym/utils') // trueisStringEmpty
Returns a boolean if the given value is an empty string.
import { isStringEmpty } from '@hypernym/utils'
isStringEmpty('') // trueisBoolean
Returns a boolean if the given value is a boolean.
import { isBoolean } from '@hypernym/utils'
isBoolean(true) // trueisTrue
Returns a boolean if the given value is a true.
import { isTrue } from '@hypernym/utils'
isTrue(true) // trueisFalse
Returns a boolean if the given value is a false.
import { isFalse } from '@hypernym/utils'
isFalse(false) // trueisNumber
Returns a boolean if the given value is a number.
import { isNumber } from '@hypernym/utils'
isNumber(33) // trueisArray
Returns a boolean if the given value is a array.
import { isArray } from '@hypernym/utils'
isArray([]) // trueisArrayEmpty
Returns a boolean if the given value is an empty array.
import { isArrayEmpty } from '@hypernym/utils'
isArrayEmpty([]) // trueisObject
Returns a boolean if the given value is a object.
import { isObject } from '@hypernym/utils'
isObject({}) // trueisObjectEmpty
Returns a boolean if the given value is an empty object.
import { isObjectEmpty } from '@hypernym/utils'
isObjectEmpty({}) // trueisFunction
Returns a boolean if the given value is a Function.
import { isFunction } from '@hypernym/utils'
isFunction(() => {}) // trueisNanValue
Returns a boolean if the given value is a NaN.
import { isNaNValue } from '@hypernym/utils'
isNaNValue(NaN) // trueisRegExp
Returns a boolean if the given value is a RegExp.
import { isRegExp } from '@hypernym/utils'
isRegExp(/^hypernym/) // trueisMap
Returns a boolean if the given value is a Map.
import { isMap } from '@hypernym/utils'
isMap(new Map()) // trueisWeakMap
Returns a boolean if the given value is a WeakMap.
import { isWeakMap } from '@hypernym/utils'
isWeakMap(new WeakMap()) // trueisSet
Returns a boolean if the given value is a Set.
import { isSet } from '@hypernym/utils'
isSet(new Set()) // trueisWeakSet
Returns a boolean if the given value is a WeakSet.
import { isWeakSet } from '@hypernym/utils'
isWeakSet(new WeakSet()) // trueisSymbol
Returns a boolean if the given value is a symbol.
import { isSymbol } from '@hypernym/utils'
isSymbol(Symboly('hypernym')) // trueisDate
Returns a boolean if the given value is a Date.
import { isDate } from '@hypernym/utils'
isDate(new Date()) // trueisBigInt
Returns a boolean if the given value is a bigint.
import { isBigInt } from '@hypernym/utils'
isBigInt(1n) // trueisInfinity
Returns a boolean if the given value is a Infinity.
import { isInfinity } from '@hypernym/utils'
isInfinity(Infinity) // trueisURL
Returns a boolean if the given value is a URL.
import { isURL } from '@hypernym/utils'
isURL(new URL('https://localhost:3000')) // trueisError
Returns a boolean if the given value is a Error.
import { isError } from '@hypernym/utils'
isError(new Error()) // trueisPrimitive
Returns a boolean if the given value is a Primitive.
import { isPrimitive } from '@hypernym/utils'
isPrimitive(true) // trueisElement
Returns a boolean if the given value is a Element.
import { isElement } from '@hypernym/utils'
isElement(el) // trueisHtmlElement
Returns a boolean if the given value is a HTMLElement.
import { isHtmlElement } from '@hypernym/utils'
isHtmlElement(htmlEl) // trueisSvgElement
Returns a boolean if the given value is a SVGElement.
import { isSvgElement } from '@hypernym/utils'
isSvgElement(svgEl) // trueisNodeList
Returns a boolean if the given value is a NodeList.
import { isNodeList } from '@hypernym/utils'
isNodeList(document.querySelectorAll('div')) // trueisNodeListEmpty
Returns a boolean if the given value is an empty NodeList.
import { isNodeListEmpty } from '@hypernym/utils'
isNodeListEmpty(document.querySelectorAll('divs')) // trueisHtmlCollection
Returns a boolean if the given value is a HTMLCollection.
import { isHtmlCollection } from '@hypernym/utils'
isHtmlCollection(document.getElementsByClassName('el')) // trueisHtmlCollectionEmpty
Returns a boolean if the given value is an empty HTMLCollection.
import { isHtmlCollectionEmpty } from '@hypernym/utils'
isHtmlCollectionEmpty(document.getElementsByClassName('els')) // truefs
exists
Checks if the file or directory exists.
import { exists } from '@hypernym/utils/fs'
await exists('dir/file.ts') // trueread
Reads the entire contents of a file.
import { read } from '@hypernym/utils/fs'
await read('dir/subdir/file.ts')readdir
Reads the contents of a directory recursively.
import { readdir } from '@hypernym/utils/fs'
await readdir('dir/subdir')write
Writes data to a file recursively.
import { write } from '@hypernym/utils/fs'
await write('dir/subdir/file.ts', `console.log('Hello World!')`)copy
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')mkdir
Creates a directory recursively.
Accepts a single path or a range of paths.
import { mkdir } from '@hypernym/utils/fs'
await mkdir('src/subdir')remove
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')Types
Primitive
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 | numberBuiltIn
Matches 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 | RegExpIsNull
Returns 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> // falseIsAny
Returns 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> // falseIsNever
Returns 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> // falsePartialDeep
Constructs 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 }>RequiredDeep
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 }>License
Developed in 🇭🇷 Croatia, © Hypernym Studio.
Released under the MIT license.
