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.
Browse files
Browse the repository at this point in the history
…ilter Feature/#499 function usage on filter
- Loading branch information
Showing
11 changed files
with
170 additions
and
84 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
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 './maskToRegExpMask'; | ||
import { isString } from './utils'; | ||
import parseMask from './utils/parseMask'; | ||
import extendMaskReplacers from './utils/extendMaskReplacers'; | ||
|
||
/** | ||
* Vue filter definition | ||
* @param {String} 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { createDirective } from './directive'; | ||
import filter from './filter'; | ||
import { createFilter } from './filter'; | ||
|
||
/** | ||
* Vue plugin definition | ||
* @param {Vue} Vue | ||
* @param {Object} options | ||
* @param {Object.<string, RegExp>} options.placeholders | ||
* @param {Object} options | ||
* @param {MaskReplaces} options.placeholders | ||
*/ | ||
export default (Vue, options = {}) => { | ||
Vue.directive('mask', createDirective(options)); | ||
Vue.filter('VMask', filter); | ||
Vue.filter('VMask', createFilter(options)); | ||
}; |
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,25 @@ | ||
import { defaultMaskReplacers } from '../constants'; | ||
|
||
/** | ||
* Merge custom mask replacers with default mask replacers | ||
* @param {MaskReplaces} maskReplacers | ||
* @param {MaskReplaces} baseMaskReplacers | ||
* @return {MaskReplaces} The extended mask replacers | ||
*/ | ||
function extendMaskReplacers(maskReplacers, baseMaskReplacers = defaultMaskReplacers) { | ||
if (maskReplacers === null || Array.isArray(maskReplacers) || typeof maskReplacers !== 'object') { | ||
return baseMaskReplacers; | ||
} | ||
|
||
return Object.keys(maskReplacers).reduce((extendedMaskReplacers, key) => { | ||
const value = maskReplacers[key]; | ||
|
||
if (value !== null && !(value instanceof RegExp)) { | ||
return extendedMaskReplacers; | ||
} | ||
|
||
return { ...extendedMaskReplacers, [key]: value }; | ||
}, baseMaskReplacers); | ||
} | ||
|
||
export default extendMaskReplacers; |
Oops, something went wrong.