From 4d7cd124b68faa36d5f527721f9008a99c46786b Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 11 Jun 2019 20:08:56 -0400 Subject: [PATCH] Add types for vanilla-masker (#36126) --- types/vanilla-masker/index.d.ts | 50 +++++++++++++++++++ types/vanilla-masker/tsconfig.json | 24 +++++++++ types/vanilla-masker/tslint.json | 1 + types/vanilla-masker/vanilla-masker-tests.ts | 52 ++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 types/vanilla-masker/index.d.ts create mode 100644 types/vanilla-masker/tsconfig.json create mode 100644 types/vanilla-masker/tslint.json create mode 100644 types/vanilla-masker/vanilla-masker-tests.ts diff --git a/types/vanilla-masker/index.d.ts b/types/vanilla-masker/index.d.ts new file mode 100644 index 0000000000..48cf0acc55 --- /dev/null +++ b/types/vanilla-masker/index.d.ts @@ -0,0 +1,50 @@ +// Type definitions for vanilla-masker 1.2 +// Project: https://fleury.io/vanilla-masker/ +// Definitions by: 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): { + 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; diff --git a/types/vanilla-masker/tsconfig.json b/types/vanilla-masker/tsconfig.json new file mode 100644 index 0000000000..6643b792ca --- /dev/null +++ b/types/vanilla-masker/tsconfig.json @@ -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" + ] +} diff --git a/types/vanilla-masker/tslint.json b/types/vanilla-masker/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/vanilla-masker/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/vanilla-masker/vanilla-masker-tests.ts b/types/vanilla-masker/vanilla-masker-tests.ts new file mode 100644 index 0000000000..5b1455d444 --- /dev/null +++ b/types/vanilla-masker/vanilla-masker-tests.ts @@ -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();