-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utilities): added match and isString utilities
- Loading branch information
1 parent
406c4ee
commit 8c9849b
Showing
5 changed files
with
69 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters