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/utils
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.
<script type="module">
import { isNull, isString, ... } from 'https://unpkg.com/@hypernym/utils/dist/index.min.mjs'
</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 // true
Returns a boolean if the given value is a null
.
import { isNull } from '@hypernym/utils'
isNull(null) // true
Returns a boolean if the given value is a undefined
.
import { isUndefined } from '@hypernym/utils'
isUndefined(undefined) // true
Returns a boolean if the given value is a string
.
import { isString } from '@hypernym/utils'
isString('@hypernym/utils') // true
Returns a boolean if the given value is an empty string
.
import { isStringEmpty } from '@hypernym/utils'
isStringEmpty('') // true
Returns a boolean if the given value is a boolean
.
import { isBoolean } from '@hypernym/utils'
isBoolean(true) // true
Returns a boolean if the given value is a true
.
import { isTrue } from '@hypernym/utils'
isTrue(true) // true
Returns a boolean if the given value is a false
.
import { isFalse } from '@hypernym/utils'
isFalse(false) // true
Returns a boolean if the given value is a number
.
import { isNumber } from '@hypernym/utils'
isNumber(33) // true
Returns a boolean if the given value is a array
.
import { isArray } from '@hypernym/utils'
isArray([]) // true
Returns a boolean if the given value is an empty array
.
import { isArrayEmpty } from '@hypernym/utils'
isArrayEmpty([]) // true
Returns a boolean if the given value is a object
.
import { isObject } from '@hypernym/utils'
isObject({}) // true
Returns a boolean if the given value is an empty object
.
import { isObjectEmpty } from '@hypernym/utils'
isObjectEmpty({}) // true
Returns a boolean if the given value is a Function
.
import { isFunction } from '@hypernym/utils'
isFunction(() => {}) // true
Returns a boolean if the given value is a NaN
.
import { isNaNValue } from '@hypernym/utils'
isNaNValue(NaN) // true
Returns a boolean if the given value is a RegExp
.
import { isRegExp } from '@hypernym/utils'
isRegExp(/^hypernym/) // true
Returns a boolean if the given value is a Map
.
import { isMap } from '@hypernym/utils'
isMap(new Map()) // true
Returns a boolean if the given value is a WeakMap
.
import { isWeakMap } from '@hypernym/utils'
isWeakMap(new WeakMap()) // true
Returns a boolean if the given value is a Set
.
import { isSet } from '@hypernym/utils'
isSet(new Set()) // true
Returns a boolean if the given value is a WeakSet
.
import { isWeakSet } from '@hypernym/utils'
isWeakSet(new WeakSet()) // true
Returns a boolean if the given value is a symbol
.
import { isSymbol } from '@hypernym/utils'
isSymbol(Symboly('hypernym')) // true
Returns a boolean if the given value is a Date
.
import { isDate } from '@hypernym/utils'
isDate(new Date()) // true
Returns a boolean if the given value is a bigint
.
import { isBigInt } from '@hypernym/utils'
isBigInt(1n) // true
Returns a boolean if the given value is a Infinity
.
import { isInfinity } from '@hypernym/utils'
isInfinity(Infinity) // true
Returns a boolean if the given value is a URL
.
import { isURL } from '@hypernym/utils'
isURL(new URL('https://localhost:3000')) // true
Returns a boolean if the given value is a Error
.
import { isError } from '@hypernym/utils'
isError(new Error()) // true
Returns a boolean if the given value is a Primitive
.
import { isPrimitive } from '@hypernym/utils'
isPrimitive(true) // true
Returns a boolean if the given value is a Element
.
import { isElement } from '@hypernym/utils'
isElement(el) // true
Returns a boolean if the given value is a HTMLElement
.
import { isHtmlElement } from '@hypernym/utils'
isHtmlElement(htmlEl) // true
Returns a boolean if the given value is a SVGElement
.
import { isSvgElement } from '@hypernym/utils'
isSvgElement(svgEl) // true
Returns a boolean if the given value is a NodeList
.
import { isNodeList } from '@hypernym/utils'
isNodeList(document.querySelectorAll('div')) // true
Returns a boolean if the given value is an empty NodeList
.
import { isNodeListEmpty } from '@hypernym/utils'
isNodeListEmpty(document.querySelectorAll('divs')) // true
Returns a boolean if the given value is a HTMLCollection
.
import { isHtmlCollection } from '@hypernym/utils'
isHtmlCollection(document.getElementsByClassName('el')) // true
Returns a boolean if the given value is an empty HTMLCollection
.
import { isHtmlCollectionEmpty } from '@hypernym/utils'
isHtmlCollectionEmpty(document.getElementsByClassName('els')) // true
Checks if the file
or directory
exists.
import { exists } from '@hypernym/utils/fs'
await exists('dir/file.ts') // true
Reads 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 | number
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 | RegExp
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> // false
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> // false
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> // false
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 }>
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.