This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(filter): make filter work with RegExps, functions and respect cu…
…stom replacers Implements: #499
- Loading branch information
Showing
3 changed files
with
60 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,30 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import conformToMask from 'text-mask-core/src/conformToMask'; | ||
import { stringMaskToRegExpMask } from './utils/maskToRegExpMask'; | ||
import { isString } from './utils'; | ||
import parseMask from './utils/parseMask'; | ||
import extendMaskReplacers from './utils/extendMaskReplacers'; | ||
|
||
/** | ||
* Vue filter definition | ||
* @param {string|number} value | ||
* @param {string} stringMask | ||
* Create the Vue filter | ||
* @param {Object} filterOptions | ||
* @param {MaskReplaces} filterOptions.placeholders | ||
*/ | ||
export default (value, stringMask) => { | ||
const mask = stringMaskToRegExpMask(stringMask); | ||
if (!isString(value) && !Number.isFinite(value)) return value; | ||
const { conformedValue } = conformToMask(`${value}`, mask, { guide: false }); | ||
return conformedValue; | ||
}; | ||
export function createFilter(filterOptions = {}) { | ||
const instanceMaskReplacers = extendMaskReplacers( | ||
filterOptions && filterOptions.placeholders, | ||
); | ||
|
||
/** | ||
* Vue filter definition | ||
* @param {string|number} value | ||
* @param {string|Array.<string|RegExp>|Function|null} inputMask | ||
*/ | ||
return (value, inputMask) => { | ||
if (!isString(value) && !Number.isFinite(value)) return value; | ||
const mask = parseMask(inputMask, instanceMaskReplacers); | ||
const { conformedValue } = conformToMask(`${value}`, mask, { guide: false }); | ||
return conformedValue; | ||
}; | ||
} | ||
|
||
export default createFilter(); |
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