forked from nuxt/image
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
100 additions
and
7 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
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
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
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,59 @@ | ||
import { AllowList } from 'types' | ||
|
||
const REGEX_RULES = [ | ||
{ matcher: /[\\$.|*+(){^]/g, replacer: match => `\\${match}` } | ||
] | ||
|
||
const regexCache = {} | ||
function makeRegex (pattern: string | RegExp, ignorecase: boolean): RegExp { | ||
if (pattern instanceof RegExp) { | ||
return pattern | ||
} | ||
|
||
if (!regexCache[pattern]) { | ||
const source = REGEX_RULES.reduce( | ||
(prev, { matcher, replacer }) => prev.replace(matcher, replacer), | ||
pattern | ||
) | ||
|
||
regexCache[pattern] = ignorecase | ||
? new RegExp(source, 'i') | ||
: new RegExp(source) | ||
} | ||
return regexCache[pattern] | ||
} | ||
|
||
function allowFunction (options: any, ignorecase: boolean) { | ||
if (typeof options === 'function') { | ||
return options | ||
} | ||
|
||
if (typeof options === 'string') { | ||
return allowFunction([options], ignorecase) | ||
} | ||
|
||
if (Array.isArray(options)) { | ||
const patterns = options.map(option => makeRegex(option, ignorecase)) | ||
return (value) => { | ||
return !!patterns.some(pattern => pattern.test(value)) | ||
} | ||
} | ||
throw new Error('Unsupported options') | ||
} | ||
|
||
export function allowList (options, ignorecase: boolean = false): AllowList { | ||
const allow = { | ||
accept: (_value: string) => true, | ||
reject: (_value: string) => false, | ||
allow: (value: string) => allow.accept(value) && !allow.reject(value) | ||
} | ||
|
||
if (options && (options.accept || options.reject)) { | ||
allow.accept = options.accept ? allowFunction(options.accept, ignorecase) : allow.accept | ||
allow.reject = options.reject ? allowFunction(options.reject, ignorecase) : allow.reject | ||
} else { | ||
allow.accept = allowFunction(options, ignorecase) | ||
} | ||
|
||
return allow | ||
} |
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
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
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
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
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