From 5358bfd10aed37bcd5752d3388af0a29f06819c0 Mon Sep 17 00:00:00 2001 From: avidenic Date: Wed, 4 Jan 2017 23:33:32 +0100 Subject: [PATCH] added typings for jQuery Mask plugin (#13706) * added typings for jQuery Mask plugin * turned on null strict checking * type fix * added missing semicolon --- jquery-mask-plugin/index.d.ts | 75 +++++++++++ .../jquery-mask-plugin-tests.ts | 119 ++++++++++++++++++ jquery-mask-plugin/tsconfig.json | 20 +++ jquery-mask-plugin/tslint.json | 3 + 4 files changed, 217 insertions(+) create mode 100644 jquery-mask-plugin/index.d.ts create mode 100644 jquery-mask-plugin/jquery-mask-plugin-tests.ts create mode 100644 jquery-mask-plugin/tsconfig.json create mode 100644 jquery-mask-plugin/tslint.json diff --git a/jquery-mask-plugin/index.d.ts b/jquery-mask-plugin/index.d.ts new file mode 100644 index 0000000000..61f3a69359 --- /dev/null +++ b/jquery-mask-plugin/index.d.ts @@ -0,0 +1,75 @@ +// Type definitions for jQuery Mask Plugin 1.14 +// Project: https://igorescobar.github.io/jQuery-Mask-Plugin/ +// Definitions by: Anže Videnič , Igor Escobar +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare namespace jQueryMask { + + interface Invalid { + p: number; + v: string; + e: string; + } + + interface Options { + maskElements?: string; + dataMaskAttr?: string; + dataMask?: boolean; + watchInterval?: number; + watchInputs?: boolean; + watchDataMask?: boolean; + byPassKeys?: number[]; + translation?: Translation; + selectOnFocus?: boolean; + reverse?: boolean; + clearIfNotMatch?: boolean; + onComplete?(value: string, e: Event, $element: JQuery, options: Options): void; + onKeyPress?(value: string, e: Event, $element: JQuery, options: Options): void; + onChange?(value: string, e: Event, $element: JQuery, options: Options): void; + onInvalid?(value: string, e: Event, $element: JQuery, invalid: Invalid[], options: Options): void; + } + + interface Pattern { + pattern?: RegExp; + recursive?: boolean; + optional?: boolean; + fallback?: string; + } + + interface Translation { + [key: string]: Pattern | {} | undefined; + placeholder?: string; + } + +} + +interface JQuery { + + /** + * Applies the mask to the matching selector elements. + * @param mask should be a string or a function. + * @param options should be an options object. + * @returns The element. + */ + mask(mask: ((value: string) => string) | string, options?: jQueryMask.Options): JQuery; + + /** + * Seek and destroy. + * @returns The element. + */ + unmask(): JQuery; + + /** + * Gets the value of the field without the mask. + * @returns Value without the mask. + */ + cleanVal(): string; + + /** + * Gets masked value programmatically + * @returns Masked value. + */ + masked(value: string): string; +} \ No newline at end of file diff --git a/jquery-mask-plugin/jquery-mask-plugin-tests.ts b/jquery-mask-plugin/jquery-mask-plugin-tests.ts new file mode 100644 index 0000000000..269bc0719e --- /dev/null +++ b/jquery-mask-plugin/jquery-mask-plugin-tests.ts @@ -0,0 +1,119 @@ + +// basic usage +$(document).ready(function () { + $('.date').mask('00/00/0000'); + $('.time').mask('00:00:00'); + $('.date_time').mask('00/00/0000 00:00:00'); + $('.cep').mask('00000-000'); + $('.phone').mask('0000-0000'); + $('.phone_with_ddd').mask('(00) 0000-0000'); + $('.phone_us').mask('(000) 000-0000'); + $('.mixed').mask('AAA 000-S0S'); + $('.cpf').mask('000.000.000-00', { reverse: true }); + $('.cnpj').mask('00.000.000/0000-00', { reverse: true }); + $('.money').mask('000.000.000.000.000,00', { reverse: true }); + $('.money2').mask("#.##0,00", { reverse: true }); + $('.ip_address').mask('0ZZ.0ZZ.0ZZ.0ZZ', { + translation: { + 'Z': { + pattern: /[0-9]/, optional: true + } + } + }); + $('.ip_address').mask('099.099.099.099'); + $('.percent').mask('##0,00%', { reverse: true }); + $('.clear-if-not-match').mask("00/00/0000", { clearIfNotMatch: true }); + $('.placeholder').mask("00/00/0000", { + translation: { + placeholder: "__/__/____" + } + }); + $('.fallback').mask("00r00r0000", { + translation: { + 'r': { + pattern: /[\/]/, + fallback: '/' + }, + placeholder: "__/__/____" + } + }); + $('.selectonfocus').mask("00/00/0000", { selectOnFocus: true }); +}); + +// callback examples +let options: jQueryMask.Options = { + onComplete: function (cep: Object) { + alert('CEP Completed!:' + cep); + }, + onKeyPress: function (cep: Object, event: Event, currentField: JQuery, options: jQueryMask.Options) { + console.log('An key was pressed!:', cep, ' event: ', event, + 'currentField: ', currentField, ' options: ', options); + }, + onChange: function (cep: Object) { + console.log('cep changed! ', cep); + }, + onInvalid: function (val: Object, e: Event, f: JQuery, invalid: jQueryMask.Invalid[], options: jQueryMask.Options) { + var error = invalid[0]; + console.log("Digit: ", error.v, " is invalid for the position: ", error.p, ". We expect something like: ", error.e); + } +}; + +$('.cep_with_callback').mask('00000-000', options); + +// on the fly mask changed +options = { + onKeyPress: function (cep, e, field, options) { + let masks: string[] = ['00000-000', '0-00-00-00']; + let mask: string = (cep.length > 7) ? masks[1] : masks[0]; + $('.crazy_cep').mask(mask, options); + } +}; + +$('.crazy_cep').mask('00000-000', options); + +// mask as a function +let maskBehavior: (value: string) => string = function (val: string) { + return val.replace(/\D/g, '').length === 11 ? '(00) 00000-0000' : '(00) 0000-00009'; +}; + +options = { + onKeyPress: function(val: string, e: Event, field: JQuery, options: jQueryMask.Options) { + field.mask(maskBehavior.apply({}, arguments), options); + } +}; + +$('.sp_celphones').mask(maskBehavior, options); + +// translation +// now the digit 0 on your mask pattern will be interpreted +// as valid characters like 0,1,2,3,4,5,6,7,8,9 and * +$('.your-field').mask('00/00/0000', {'translation': {0: {pattern: /[0-9*]/}}}); + +// optional digits +// way 1 +$('.ip_address').mask('099.099.099.099'); +// way 2 +$('.ip_address').mask('0ZZ.0ZZ.0ZZ.0ZZ', {translation: {'Z': {pattern: /[0-9]/, optional: true}}}); + +// recursive digits +$('.money_example').mask('#.##0,00', {reverse: true}); + +// fallback digits +$('.field_with_fallback').mask("00r00r0000", { + translation: { + 'r': { + pattern: /[\/]/, + fallback: '/' + }, + placeholder: "__/__/____" + } +}); + +// Removing the mask +$('.date').unmask(); + +// Getting the unmasked typed value +$('.date').cleanVal(); + +// Getting a masked value programmatically +$('.date').masked('19840807'); \ No newline at end of file diff --git a/jquery-mask-plugin/tsconfig.json b/jquery-mask-plugin/tsconfig.json new file mode 100644 index 0000000000..e805aac8db --- /dev/null +++ b/jquery-mask-plugin/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "jquery-mask-plugin-tests.ts" + ] +} \ No newline at end of file diff --git a/jquery-mask-plugin/tslint.json b/jquery-mask-plugin/tslint.json new file mode 100644 index 0000000000..192203ab54 --- /dev/null +++ b/jquery-mask-plugin/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "../tslint.json" +} \ No newline at end of file