From c46de29c7942bab9560559c9d278bcb0373c6f0e Mon Sep 17 00:00:00 2001 From: RamIdeas <5822355+RamIdeas@users.noreply.github.com> Date: Fri, 6 Nov 2020 18:04:58 +0000 Subject: [PATCH 1/3] test for function with `typeof` not `instanceof` allows environments (e.g. jest) to override the global Function constructor --- src/tokeniser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tokeniser.ts b/src/tokeniser.ts index af7b215..f330a6e 100644 --- a/src/tokeniser.ts +++ b/src/tokeniser.ts @@ -21,7 +21,7 @@ const tokenise = (str: string, tokens: Token[] = []): Token[] => { match: match[0], val: match.slice(1, 2), otherVal: match.slice(2), - regex: rule.regex instanceof Function ? rule.regex(match) : rule.regex + regex: typeof rule.regex === 'function' ? rule.regex(match) : rule.regex }) if (match[0].length < str.length) { From 71dc258ba4cf0d77c3f5b7c56a394fc43b0b7c05 Mon Sep 17 00:00:00 2001 From: RamIdeas <5822355+RamIdeas@users.noreply.github.com> Date: Fri, 13 Nov 2020 01:41:04 +0000 Subject: [PATCH 2/3] check for RegExp duck-type using `constructor.name` rather than `instanceof` --- src/Path.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Path.ts b/src/Path.ts index 2e7159e..ae227b3 100644 --- a/src/Path.ts +++ b/src/Path.ts @@ -329,7 +329,7 @@ export class Path = Record> { private getParams(type: string | RegExp): string[] { const predicate = - type instanceof RegExp + type.constructor.name === 'RegExp' ? (t: Token) => type.test(t.type) : (t: Token) => t.type === type From c5f4e6d920c3d43a2a886c2891134b26a374aa7d Mon Sep 17 00:00:00 2001 From: RamIdeas <5822355+RamIdeas@users.noreply.github.com> Date: Fri, 13 Nov 2020 01:47:11 +0000 Subject: [PATCH 3/3] differentiate regex using `typeof string` rather than `instanceof RegExp` --- src/Path.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Path.ts b/src/Path.ts index ae227b3..38e0ecf 100644 --- a/src/Path.ts +++ b/src/Path.ts @@ -329,7 +329,7 @@ export class Path = Record> { private getParams(type: string | RegExp): string[] { const predicate = - type.constructor.name === 'RegExp' + typeof type !== 'string' ? (t: Token) => type.test(t.type) : (t: Token) => t.type === type