@make-mjs/utils
Advanced tools
| export declare type MaybePromise<X> = X | Promise<X>; | ||
| export declare const REJECTION_SILENCER: () => void; | ||
| /** | ||
| * Silence a unhandled promise rejection warning | ||
| * @param promise Promise to silence | ||
| */ | ||
| export declare function silenceRejection<X>(promise: Promise<X>): Promise<X>; | ||
| /** | ||
| * Remove trailing `'/'` if there is one | ||
| * @param path Path to be rid of trailing separator | ||
| */ | ||
| export declare function removeTrailingSeparator(path: string): string; | ||
| /** | ||
| * Like `path.join` but does not eliminate `'.'` | ||
| * @param paths Paths to join together | ||
| */ | ||
| export declare function joinPath(...paths: string[]): string; | ||
| /** | ||
| * Like `path.parse` but accept only `'/'` as separator | ||
| * @param path Path to parse | ||
| */ | ||
| export declare function parsePath(path: string): parsePath.Result; | ||
| export declare namespace parsePath { | ||
| interface Result { | ||
| dir: string; | ||
| base: string; | ||
| name: string; | ||
| ext: string; | ||
| } | ||
| } | ||
| export declare function replacePathExtension(path: string, newExt: string): string; | ||
| export declare function iterateAncestorDirectories(path: string): Generator<string, void, unknown>; | ||
| /** | ||
| * Returns a function that | ||
| * takes an `x`, | ||
| * returns `f(x)` if `f(x)` is falsy | ||
| * or `g(x)` if `f(x)` is truthy | ||
| * @param f First function to call | ||
| * @param g Second function to call | ||
| */ | ||
| export declare function fnAnd<X, Y, Z>(f: (x: X) => Y, g: (x: X) => Z): (x: X) => Y | Z; | ||
| /** | ||
| * Returns a function that | ||
| * takes an `x`, | ||
| * returns `f(x)` if `f(x)` is truthy | ||
| * or `g(x)` if `f(x)` is falsy | ||
| * @param f First function to call | ||
| * @param g Second function to call | ||
| */ | ||
| export declare function fnOr<X, Y, Z>(f: (x: X) => Y, g: (x: X) => Z): (x: X) => Y | Z; |
+38
-29
@@ -6,5 +6,6 @@ export const REJECTION_SILENCER = () => undefined; | ||
| */ | ||
| export function silenceRejection(promise) { | ||
| void promise.catch(REJECTION_SILENCER); | ||
| return promise; | ||
| void promise.catch(REJECTION_SILENCER); | ||
| return promise; | ||
| } | ||
@@ -15,6 +16,5 @@ /** | ||
| */ | ||
| export function removeTrailingSeparator(path) { | ||
| return path.endsWith('/') | ||
| ? path.slice(0, -1) | ||
| : path; | ||
| return path.endsWith('/') ? path.slice(0, -1) : path; | ||
| } | ||
@@ -25,7 +25,5 @@ /** | ||
| */ | ||
| export function joinPath(...paths) { | ||
| return paths | ||
| .map(removeTrailingSeparator) | ||
| .filter(Boolean) | ||
| .join('/'); | ||
| return paths.map(removeTrailingSeparator).filter(Boolean).join('/'); | ||
| } | ||
@@ -36,24 +34,34 @@ /** | ||
| */ | ||
| export function parsePath(path) { | ||
| if (path.endsWith('/')) | ||
| return parsePath(path.slice(0, -1)); | ||
| const segments = path.split('/'); | ||
| const base = segments.pop(); | ||
| if (!base) | ||
| return { base: '', dir: '', name: '', ext: '' }; | ||
| const dir = segments.join('/'); | ||
| const [name, extWithoutDot] = base.split('.'); | ||
| const ext = extWithoutDot === undefined ? '' : ('.' + extWithoutDot); | ||
| return { base, dir, name, ext }; | ||
| if (path.endsWith('/')) return parsePath(path.slice(0, -1)); | ||
| const segments = path.split('/'); | ||
| const base = segments.pop(); | ||
| if (!base) return { | ||
| base: '', | ||
| dir: '', | ||
| name: '', | ||
| ext: '' | ||
| }; | ||
| const dir = segments.join('/'); | ||
| const [name, extWithoutDot] = base.split('.'); | ||
| const ext = extWithoutDot === undefined ? '' : '.' + extWithoutDot; | ||
| return { | ||
| base, | ||
| dir, | ||
| name, | ||
| ext | ||
| }; | ||
| } | ||
| const EXT_REGEX = /(\.[^\.]*)?$/; | ||
| export function replacePathExtension(path, newExt) { | ||
| return path.replace(EXT_REGEX, newExt); | ||
| return path.replace(EXT_REGEX, newExt); | ||
| } | ||
| export function* iterateAncestorDirectories(path) { | ||
| const segments = path.split('/').filter(Boolean); | ||
| while (segments.length) { | ||
| segments.pop(); | ||
| yield joinPath(...segments); | ||
| } | ||
| const segments = path.split('/').filter(Boolean); | ||
| while (segments.length) { | ||
| segments.pop(); | ||
| yield joinPath(...segments); | ||
| } | ||
| } | ||
@@ -68,4 +76,5 @@ /** | ||
| */ | ||
| export function fnAnd(f, g) { | ||
| return x => f(x) && g(x); | ||
| return x => f(x) && g(x); | ||
| } | ||
@@ -80,5 +89,5 @@ /** | ||
| */ | ||
| export function fnOr(f, g) { | ||
| return x => f(x) || g(x); | ||
| } | ||
| //# sourceMappingURL=index.js.map | ||
| return x => f(x) || g(x); | ||
| } //# sourceMappingURL=index.js.map |
+1
-1
| { | ||
| "name": "@make-mjs/utils", | ||
| "version": "0.0.0", | ||
| "version": "0.0.1", | ||
| "description": "Utilities used by other packages", | ||
@@ -5,0 +5,0 @@ "author": "Hoàng Văn Khải <hvksmr1996@gmail.com>", |
8410
23.64%5
25%266
24.3%