Skip to content

Commit

Permalink
feat(utilities): added match and isString utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Nov 17, 2022
1 parent 406c4ee commit 8c9849b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/utilities/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
export { default as functionIdentity } from './function/identity';

export { default as matchGetFilter } from './match/get-filter';

export { default as objectClone } from './object/clone';
export { default as objectFromPath } from './object/from-path';
export { default as objectLock } from './object/lock';
export { default as objectOverwrite } from './object/overwrite';
export { default as objectOmit } from './object/omit';
export { default as objectFromPath } from './object/from-path';
export { default as objectOverwrite } from './object/overwrite';
export { default as objectSet } from './object/set';
export { default as objectToPath } from './object/to-path';
export { default as objectTrace } from './object/trace';
export { default as objectSet } from './object/set';

export { default as typeGetType } from './type/get-type';
export { default as typeIsArray } from './type/is-array';
export { default as typeIsFunction } from './type/is-function';
export { default as typeIsObject } from './type/is-object';
export { default as typeIsMatchable } from './type/is-matchable';
export { default as typeIsNil } from './type/is-nil';
export { default as typeIsObject } from './type/is-object';
export { default as typeIsString } from './type/is-string';

export * from './types';
34 changes: 34 additions & 0 deletions packages/utilities/src/match/get-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import isFunction from '../type/is-function';
import isString from '../type/is-string';

import {
Matchable,
Matcher,
Predicate,
} from '../types';

function normaliseMatcher(matcher: Matcher): Predicate<string> {
if (matcher === '*') {
return () => true;
}

if (isFunction(matcher)) {
return matcher;
}

const patterns = ([] as (string | RegExp)[])
.concat(matcher)
.map(pattern => isString(pattern)
? new RegExp(pattern)
: pattern
);

return value => patterns.some(pattern => pattern.test(value));
}

export default function getFilter({ include, exclude }: Matchable): Predicate<string> {
const includeMatcher = normaliseMatcher(include);
const excludeMatcher = normaliseMatcher(exclude);

return value => excludeMatcher(value) || !includeMatcher(value);
}
11 changes: 11 additions & 0 deletions packages/utilities/src/type/is-matchable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import isObject from './is-object';

import type {
Matchable,
} from '../types';

export default function isMatchable(value: unknown): value is Matchable {
return isObject(value)
&& 'include' in value
&& 'exclude' in value;
}
5 changes: 5 additions & 0 deletions packages/utilities/src/type/is-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import getType from './get-type';

export default function isString(value: unknown): value is string {
return getType(value) === 'string';
}
11 changes: 11 additions & 0 deletions packages/utilities/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
export type UnionToIntersection<TValue> = (TValue extends any ? (arg: TValue) => any : never) extends ((arg: infer I) => void) ? I : never;

export type OneOrMore<TValue> = TValue | TValue[];
export type Predicate<TValue> = (value: TValue) => boolean;
export type Matcher = OneOrMore<string | RegExp> | Predicate<string>;

export interface Constructable<TValue = unknown> {
constructor: new (...args: unknown[]) => TValue;
}

export interface Matchable {
include: Matcher;
exclude: Matcher;
}

export type RuntimeType = 'boolean'
| 'number'
| 'string'
Expand Down

0 comments on commit 8c9849b

Please sign in to comment.