Add types for vanilla-masker (#36126)

This commit is contained in:
Ben 2019-06-11 20:08:56 -04:00 committed by Ron Buckton
parent 8bc13c7029
commit 4d7cd124b6
4 changed files with 127 additions and 0 deletions

50
types/vanilla-masker/index.d.ts vendored Normal file
View File

@ -0,0 +1,50 @@
// Type definitions for vanilla-masker 1.2
// Project: https://fleury.io/vanilla-masker/
// Definitions by: BenLorantfy <https://github.com/BenLorantfy>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
interface MoneyOptions {
// Decimal precision -> "90"
precision?: number;
// Decimal separator -> ",90"
separator?: string;
// Number delimiter -> "12.345.678"
delimiter?: string;
// Money unit -> "R$ 12.345.678,90"
unit?: string;
// Money unit -> "12.345.678,90 R$"
suffixUnit?: string;
// Force type only number instead decimal,
// masking decimals with ",00"
// Zero cents -> "R$ 1.234.567.890,00"
zeroCents?: boolean;
}
interface PatternOptions {
// Pattern to mask value against.
pattern?: string;
// Placeholder option to represent remaining characters to be entered
placeholder?: string;
}
declare const VMasker: {
(el: Element|NodeListOf<Element>): {
maskMoney: (options?: MoneyOptions) => void;
maskNumber: () => void;
maskAlphaNum: () => void;
maskPattern: (pattern: string) => void;
unMask: () => void;
}
toMoney: (value: string|number, options?: MoneyOptions) => string;
toPattern: (value: string|number, options?: string|PatternOptions) => string;
toNumber: (value: string|number) => string;
toAlphaNumeric: (value: string|number) => string;
};
export = VMasker;

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"vanilla-masker-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@ -0,0 +1,52 @@
import * as VMasker from "vanilla-masker";
// Masking input element to money.
const el = document.querySelector("data-js-input") as Element;
const els = document.querySelectorAll("data-js-input");
VMasker(el).maskMoney();
VMasker(el).maskMoney({});
// Masking input element to money with options.
VMasker(el).maskMoney({
// Decimal precision -> "90"
precision: 2,
// Decimal separator -> ",90"
separator: ',',
// Number delimiter -> "12.345.678"
delimiter: '.',
// Money unit -> "R$ 12.345.678,90"
unit: 'R$',
// Money unit -> "12.345.678,90 R$"
suffixUnit: 'R$',
// Force type only number instead decimal,
// masking decimals with ",00"
// Zero cents -> "R$ 1.234.567.890,00"
zeroCents: true
});
// Masking an array of input elements to money.
VMasker(els).maskMoney();
// Converts number to money string
VMasker.toMoney(1234); // -> R$ 1.234,00
// Masking input element to number.
VMasker(el).maskNumber();
// Converts any string to number
VMasker.toNumber("123ac34"); // -> 12334
VMasker.toNumber("-123ac34"); // -> -12334
VMasker.toAlphaNumeric("-123ac34");
// Listen the input element masking it to format with pattern.
VMasker(el).maskPattern("(99) 9999-9999");
// Converts value to masked phone
VMasker.toPattern(1099911111, "(99) 9999-9999"); // -> (10) 9991-1111
// Pass in an optional placeholder option to represent remaining characters to be entered
VMasker.toPattern('4', {pattern: "(999) 999-9999", placeholder: "x"}); // -> (4xx) xxx-xxxx
// unmask!
VMasker(el).unMask();