How to use digit method in Playwright Internal

Best JavaScript code snippet using playwright-internal

asyoutypeformatter_test.js

Source:asyoutypeformatter_test.js Github

copy

Full Screen

1/**2 * @license3 * Copyright (C) 2010 The Libphonenumber Authors.4 *5 * Licensed under the Apache License, Version 2.0 (the "License");6 * you may not use this file except in compliance with the License.7 * You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17/**18 * @fileoverview Unit tests for the AsYouTypeFormatter.19 *20 * Note that these tests use the metadata contained in metadatafortesting.js,21 * not the normal metadata files, so should not be used for regression test22 * purposes - these tests are illustrative only and test functionality.23 *24 * @author Nikolaos Trogkanis25 */26goog.require('goog.testing.jsunit');27goog.require('i18n.phonenumbers.AsYouTypeFormatter');28goog.require('i18n.phonenumbers.RegionCode');29var RegionCode = i18n.phonenumbers.RegionCode;30function testInvalidRegion() {31 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */32 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.ZZ);33 assertEquals('+', f.inputDigit('+'));34 assertEquals('+4', f.inputDigit('4'));35 assertEquals('+48 ', f.inputDigit('8'));36 assertEquals('+48 8', f.inputDigit('8'));37 assertEquals('+48 88', f.inputDigit('8'));38 assertEquals('+48 88 1', f.inputDigit('1'));39 assertEquals('+48 88 12', f.inputDigit('2'));40 assertEquals('+48 88 123', f.inputDigit('3'));41 assertEquals('+48 88 123 1', f.inputDigit('1'));42 assertEquals('+48 88 123 12', f.inputDigit('2'));43 f.clear();44 assertEquals('6', f.inputDigit('6'));45 assertEquals('65', f.inputDigit('5'));46 assertEquals('650', f.inputDigit('0'));47 assertEquals('6502', f.inputDigit('2'));48 assertEquals('65025', f.inputDigit('5'));49 assertEquals('650253', f.inputDigit('3'));50}51function testInvalidPlusSign() {52 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */53 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.ZZ);54 assertEquals('+', f.inputDigit('+'));55 assertEquals('+4', f.inputDigit('4'));56 assertEquals('+48 ', f.inputDigit('8'));57 assertEquals('+48 8', f.inputDigit('8'));58 assertEquals('+48 88', f.inputDigit('8'));59 assertEquals('+48 88 1', f.inputDigit('1'));60 assertEquals('+48 88 12', f.inputDigit('2'));61 assertEquals('+48 88 123', f.inputDigit('3'));62 assertEquals('+48 88 123 1', f.inputDigit('1'));63 // A plus sign can only appear at the beginning of the number;64 // otherwise, no formatting is applied.65 assertEquals('+48881231+', f.inputDigit('+'));66 assertEquals('+48881231+2', f.inputDigit('2'));67}68function testTooLongNumberMatchingMultipleLeadingDigits() {69 // See https://github.com/google/libphonenumber/issues/3670 // The bug occurred last time for countries which have two formatting rules71 // with exactly the same leading digits pattern but differ in length.72 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */73 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.ZZ);74 assertEquals('+', f.inputDigit('+'));75 assertEquals('+8', f.inputDigit('8'));76 assertEquals('+81 ', f.inputDigit('1'));77 assertEquals('+81 9', f.inputDigit('9'));78 assertEquals('+81 90', f.inputDigit('0'));79 assertEquals('+81 90 1', f.inputDigit('1'));80 assertEquals('+81 90 12', f.inputDigit('2'));81 assertEquals('+81 90 123', f.inputDigit('3'));82 assertEquals('+81 90 1234', f.inputDigit('4'));83 assertEquals('+81 90 1234 5', f.inputDigit('5'));84 assertEquals('+81 90 1234 56', f.inputDigit('6'));85 assertEquals('+81 90 1234 567', f.inputDigit('7'));86 assertEquals('+81 90 1234 5678', f.inputDigit('8'));87 assertEquals('+81 90 12 345 6789', f.inputDigit('9'));88 assertEquals('+81901234567890', f.inputDigit('0'));89 assertEquals('+819012345678901', f.inputDigit('1'));90}91function testCountryWithSpaceInNationalPrefixFormattingRule() {92 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */93 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.BY);94 assertEquals('8', f.inputDigit('8'));95 assertEquals('88', f.inputDigit('8'));96 assertEquals('881', f.inputDigit('1'));97 assertEquals('8 819', f.inputDigit('9'));98 assertEquals('8 8190', f.inputDigit('0'));99 // The formatting rule for 5 digit numbers states that no space should be100 // present after the national prefix.101 assertEquals('881 901', f.inputDigit('1'));102 assertEquals('8 819 012', f.inputDigit('2'));103 // Too long, no formatting rule applies.104 assertEquals('88190123', f.inputDigit('3'));105}106function testCountryWithSpaceInNationalPrefixFormattingRuleAndLongNdd() {107 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */108 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.BY);109 assertEquals('9', f.inputDigit('9'));110 assertEquals('99', f.inputDigit('9'));111 assertEquals('999', f.inputDigit('9'));112 assertEquals('9999', f.inputDigit('9'));113 assertEquals('99999 ', f.inputDigit('9'));114 assertEquals('99999 1', f.inputDigit('1'));115 assertEquals('99999 12', f.inputDigit('2'));116 assertEquals('99999 123', f.inputDigit('3'));117 assertEquals('99999 1234', f.inputDigit('4'));118 assertEquals('99999 12 345', f.inputDigit('5'));119}120function testAYTFUS() {121 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */122 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.US);123 assertEquals('6', f.inputDigit('6'));124 assertEquals('65', f.inputDigit('5'));125 assertEquals('650', f.inputDigit('0'));126 assertEquals('650 2', f.inputDigit('2'));127 assertEquals('650 25', f.inputDigit('5'));128 assertEquals('650 253', f.inputDigit('3'));129 // Note this is how a US local number (without area code) should be formatted.130 assertEquals('650 2532', f.inputDigit('2'));131 assertEquals('650 253 22', f.inputDigit('2'));132 assertEquals('650 253 222', f.inputDigit('2'));133 assertEquals('650 253 2222', f.inputDigit('2'));134 f.clear();135 assertEquals('1', f.inputDigit('1'));136 assertEquals('16', f.inputDigit('6'));137 assertEquals('1 65', f.inputDigit('5'));138 assertEquals('1 650', f.inputDigit('0'));139 assertEquals('1 650 2', f.inputDigit('2'));140 assertEquals('1 650 25', f.inputDigit('5'));141 assertEquals('1 650 253', f.inputDigit('3'));142 assertEquals('1 650 253 2', f.inputDigit('2'));143 assertEquals('1 650 253 22', f.inputDigit('2'));144 assertEquals('1 650 253 222', f.inputDigit('2'));145 assertEquals('1 650 253 2222', f.inputDigit('2'));146 f.clear();147 assertEquals('0', f.inputDigit('0'));148 assertEquals('01', f.inputDigit('1'));149 assertEquals('011 ', f.inputDigit('1'));150 assertEquals('011 4', f.inputDigit('4'));151 assertEquals('011 44 ', f.inputDigit('4'));152 assertEquals('011 44 6', f.inputDigit('6'));153 assertEquals('011 44 61', f.inputDigit('1'));154 assertEquals('011 44 6 12', f.inputDigit('2'));155 assertEquals('011 44 6 123', f.inputDigit('3'));156 assertEquals('011 44 6 123 1', f.inputDigit('1'));157 assertEquals('011 44 6 123 12', f.inputDigit('2'));158 assertEquals('011 44 6 123 123', f.inputDigit('3'));159 assertEquals('011 44 6 123 123 1', f.inputDigit('1'));160 assertEquals('011 44 6 123 123 12', f.inputDigit('2'));161 assertEquals('011 44 6 123 123 123', f.inputDigit('3'));162 f.clear();163 assertEquals('0', f.inputDigit('0'));164 assertEquals('01', f.inputDigit('1'));165 assertEquals('011 ', f.inputDigit('1'));166 assertEquals('011 5', f.inputDigit('5'));167 assertEquals('011 54 ', f.inputDigit('4'));168 assertEquals('011 54 9', f.inputDigit('9'));169 assertEquals('011 54 91', f.inputDigit('1'));170 assertEquals('011 54 9 11', f.inputDigit('1'));171 assertEquals('011 54 9 11 2', f.inputDigit('2'));172 assertEquals('011 54 9 11 23', f.inputDigit('3'));173 assertEquals('011 54 9 11 231', f.inputDigit('1'));174 assertEquals('011 54 9 11 2312', f.inputDigit('2'));175 assertEquals('011 54 9 11 2312 1', f.inputDigit('1'));176 assertEquals('011 54 9 11 2312 12', f.inputDigit('2'));177 assertEquals('011 54 9 11 2312 123', f.inputDigit('3'));178 assertEquals('011 54 9 11 2312 1234', f.inputDigit('4'));179 f.clear();180 assertEquals('0', f.inputDigit('0'));181 assertEquals('01', f.inputDigit('1'));182 assertEquals('011 ', f.inputDigit('1'));183 assertEquals('011 2', f.inputDigit('2'));184 assertEquals('011 24', f.inputDigit('4'));185 assertEquals('011 244 ', f.inputDigit('4'));186 assertEquals('011 244 2', f.inputDigit('2'));187 assertEquals('011 244 28', f.inputDigit('8'));188 assertEquals('011 244 280', f.inputDigit('0'));189 assertEquals('011 244 280 0', f.inputDigit('0'));190 assertEquals('011 244 280 00', f.inputDigit('0'));191 assertEquals('011 244 280 000', f.inputDigit('0'));192 assertEquals('011 244 280 000 0', f.inputDigit('0'));193 assertEquals('011 244 280 000 00', f.inputDigit('0'));194 assertEquals('011 244 280 000 000', f.inputDigit('0'));195 f.clear();196 assertEquals('+', f.inputDigit('+'));197 assertEquals('+4', f.inputDigit('4'));198 assertEquals('+48 ', f.inputDigit('8'));199 assertEquals('+48 8', f.inputDigit('8'));200 assertEquals('+48 88', f.inputDigit('8'));201 assertEquals('+48 88 1', f.inputDigit('1'));202 assertEquals('+48 88 12', f.inputDigit('2'));203 assertEquals('+48 88 123', f.inputDigit('3'));204 assertEquals('+48 88 123 1', f.inputDigit('1'));205 assertEquals('+48 88 123 12', f.inputDigit('2'));206 assertEquals('+48 88 123 12 1', f.inputDigit('1'));207 assertEquals('+48 88 123 12 12', f.inputDigit('2'));208}209function testAYTFUSFullWidthCharacters() {210 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */211 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.US);212 assertEquals('\uFF16', f.inputDigit('\uFF16'));213 assertEquals('\uFF16\uFF15', f.inputDigit('\uFF15'));214 assertEquals('650', f.inputDigit('\uFF10'));215 assertEquals('650 2', f.inputDigit('\uFF12'));216 assertEquals('650 25', f.inputDigit('\uFF15'));217 assertEquals('650 253', f.inputDigit('\uFF13'));218 assertEquals('650 2532', f.inputDigit('\uFF12'));219 assertEquals('650 253 22', f.inputDigit('\uFF12'));220 assertEquals('650 253 222', f.inputDigit('\uFF12'));221 assertEquals('650 253 2222', f.inputDigit('\uFF12'));222}223function testAYTFUSMobileShortCode() {224 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */225 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.US);226 assertEquals('*', f.inputDigit('*'));227 assertEquals('*1', f.inputDigit('1'));228 assertEquals('*12', f.inputDigit('2'));229 assertEquals('*121', f.inputDigit('1'));230 assertEquals('*121#', f.inputDigit('#'));231}232function testAYTFUSVanityNumber() {233 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */234 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.US);235 assertEquals('8', f.inputDigit('8'));236 assertEquals('80', f.inputDigit('0'));237 assertEquals('800', f.inputDigit('0'));238 assertEquals('800 ', f.inputDigit(' '));239 assertEquals('800 M', f.inputDigit('M'));240 assertEquals('800 MY', f.inputDigit('Y'));241 assertEquals('800 MY ', f.inputDigit(' '));242 assertEquals('800 MY A', f.inputDigit('A'));243 assertEquals('800 MY AP', f.inputDigit('P'));244 assertEquals('800 MY APP', f.inputDigit('P'));245 assertEquals('800 MY APPL', f.inputDigit('L'));246 assertEquals('800 MY APPLE', f.inputDigit('E'));247}248function testAYTFAndRememberPositionUS() {249 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */250 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.US);251 assertEquals('1', f.inputDigitAndRememberPosition('1'));252 assertEquals(1, f.getRememberedPosition());253 assertEquals('16', f.inputDigit('6'));254 assertEquals('1 65', f.inputDigit('5'));255 assertEquals(1, f.getRememberedPosition());256 assertEquals('1 650', f.inputDigitAndRememberPosition('0'));257 assertEquals(5, f.getRememberedPosition());258 assertEquals('1 650 2', f.inputDigit('2'));259 assertEquals('1 650 25', f.inputDigit('5'));260 // Note the remembered position for digit '0' changes from 4 to 5, because a261 // space is now inserted in the front.262 assertEquals(5, f.getRememberedPosition());263 assertEquals('1 650 253', f.inputDigit('3'));264 assertEquals('1 650 253 2', f.inputDigit('2'));265 assertEquals('1 650 253 22', f.inputDigit('2'));266 assertEquals(5, f.getRememberedPosition());267 assertEquals('1 650 253 222', f.inputDigitAndRememberPosition('2'));268 assertEquals(13, f.getRememberedPosition());269 assertEquals('1 650 253 2222', f.inputDigit('2'));270 assertEquals(13, f.getRememberedPosition());271 assertEquals('165025322222', f.inputDigit('2'));272 assertEquals(10, f.getRememberedPosition());273 assertEquals('1650253222222', f.inputDigit('2'));274 assertEquals(10, f.getRememberedPosition());275 f.clear();276 assertEquals('1', f.inputDigit('1'));277 assertEquals('16', f.inputDigitAndRememberPosition('6'));278 assertEquals(2, f.getRememberedPosition());279 assertEquals('1 65', f.inputDigit('5'));280 assertEquals('1 650', f.inputDigit('0'));281 assertEquals(3, f.getRememberedPosition());282 assertEquals('1 650 2', f.inputDigit('2'));283 assertEquals('1 650 25', f.inputDigit('5'));284 assertEquals(3, f.getRememberedPosition());285 assertEquals('1 650 253', f.inputDigit('3'));286 assertEquals('1 650 253 2', f.inputDigit('2'));287 assertEquals('1 650 253 22', f.inputDigit('2'));288 assertEquals(3, f.getRememberedPosition());289 assertEquals('1 650 253 222', f.inputDigit('2'));290 assertEquals('1 650 253 2222', f.inputDigit('2'));291 assertEquals('165025322222', f.inputDigit('2'));292 assertEquals(2, f.getRememberedPosition());293 assertEquals('1650253222222', f.inputDigit('2'));294 assertEquals(2, f.getRememberedPosition());295 f.clear();296 assertEquals('6', f.inputDigit('6'));297 assertEquals('65', f.inputDigit('5'));298 assertEquals('650', f.inputDigit('0'));299 assertEquals('650 2', f.inputDigit('2'));300 assertEquals('650 25', f.inputDigit('5'));301 assertEquals('650 253', f.inputDigit('3'));302 assertEquals('650 2532', f.inputDigitAndRememberPosition('2'));303 assertEquals(8, f.getRememberedPosition());304 assertEquals('650 253 22', f.inputDigit('2'));305 assertEquals(9, f.getRememberedPosition());306 assertEquals('650 253 222', f.inputDigit('2'));307 // No more formatting when semicolon is entered.308 assertEquals('650253222;', f.inputDigit(';'));309 assertEquals(7, f.getRememberedPosition());310 assertEquals('650253222;2', f.inputDigit('2'));311 f.clear();312 assertEquals('6', f.inputDigit('6'));313 assertEquals('65', f.inputDigit('5'));314 assertEquals('650', f.inputDigit('0'));315 // No more formatting when users choose to do their own formatting.316 assertEquals('650-', f.inputDigit('-'));317 assertEquals('650-2', f.inputDigitAndRememberPosition('2'));318 assertEquals(5, f.getRememberedPosition());319 assertEquals('650-25', f.inputDigit('5'));320 assertEquals(5, f.getRememberedPosition());321 assertEquals('650-253', f.inputDigit('3'));322 assertEquals(5, f.getRememberedPosition());323 assertEquals('650-253-', f.inputDigit('-'));324 assertEquals('650-253-2', f.inputDigit('2'));325 assertEquals('650-253-22', f.inputDigit('2'));326 assertEquals('650-253-222', f.inputDigit('2'));327 assertEquals('650-253-2222', f.inputDigit('2'));328 f.clear();329 assertEquals('0', f.inputDigit('0'));330 assertEquals('01', f.inputDigit('1'));331 assertEquals('011 ', f.inputDigit('1'));332 assertEquals('011 4', f.inputDigitAndRememberPosition('4'));333 assertEquals('011 48 ', f.inputDigit('8'));334 assertEquals(5, f.getRememberedPosition());335 assertEquals('011 48 8', f.inputDigit('8'));336 assertEquals(5, f.getRememberedPosition());337 assertEquals('011 48 88', f.inputDigit('8'));338 assertEquals('011 48 88 1', f.inputDigit('1'));339 assertEquals('011 48 88 12', f.inputDigit('2'));340 assertEquals(5, f.getRememberedPosition());341 assertEquals('011 48 88 123', f.inputDigit('3'));342 assertEquals('011 48 88 123 1', f.inputDigit('1'));343 assertEquals('011 48 88 123 12', f.inputDigit('2'));344 assertEquals('011 48 88 123 12 1', f.inputDigit('1'));345 assertEquals('011 48 88 123 12 12', f.inputDigit('2'));346 f.clear();347 assertEquals('+', f.inputDigit('+'));348 assertEquals('+1', f.inputDigit('1'));349 assertEquals('+1 6', f.inputDigitAndRememberPosition('6'));350 assertEquals('+1 65', f.inputDigit('5'));351 assertEquals('+1 650', f.inputDigit('0'));352 assertEquals(4, f.getRememberedPosition());353 assertEquals('+1 650 2', f.inputDigit('2'));354 assertEquals(4, f.getRememberedPosition());355 assertEquals('+1 650 25', f.inputDigit('5'));356 assertEquals('+1 650 253', f.inputDigitAndRememberPosition('3'));357 assertEquals('+1 650 253 2', f.inputDigit('2'));358 assertEquals('+1 650 253 22', f.inputDigit('2'));359 assertEquals('+1 650 253 222', f.inputDigit('2'));360 assertEquals(10, f.getRememberedPosition());361 f.clear();362 assertEquals('+', f.inputDigit('+'));363 assertEquals('+1', f.inputDigit('1'));364 assertEquals('+1 6', f.inputDigitAndRememberPosition('6'));365 assertEquals('+1 65', f.inputDigit('5'));366 assertEquals('+1 650', f.inputDigit('0'));367 assertEquals(4, f.getRememberedPosition());368 assertEquals('+1 650 2', f.inputDigit('2'));369 assertEquals(4, f.getRememberedPosition());370 assertEquals('+1 650 25', f.inputDigit('5'));371 assertEquals('+1 650 253', f.inputDigit('3'));372 assertEquals('+1 650 253 2', f.inputDigit('2'));373 assertEquals('+1 650 253 22', f.inputDigit('2'));374 assertEquals('+1 650 253 222', f.inputDigit('2'));375 assertEquals('+1650253222;', f.inputDigit(';'));376 assertEquals(3, f.getRememberedPosition());377}378function testAYTFGBFixedLine() {379 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */380 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.GB);381 assertEquals('0', f.inputDigit('0'));382 assertEquals('02', f.inputDigit('2'));383 assertEquals('020', f.inputDigit('0'));384 assertEquals('020 7', f.inputDigitAndRememberPosition('7'));385 assertEquals(5, f.getRememberedPosition());386 assertEquals('020 70', f.inputDigit('0'));387 assertEquals('020 703', f.inputDigit('3'));388 assertEquals(5, f.getRememberedPosition());389 assertEquals('020 7031', f.inputDigit('1'));390 assertEquals('020 7031 3', f.inputDigit('3'));391 assertEquals('020 7031 30', f.inputDigit('0'));392 assertEquals('020 7031 300', f.inputDigit('0'));393 assertEquals('020 7031 3000', f.inputDigit('0'));394}395function testAYTFGBTollFree() {396 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */397 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.GB);398 assertEquals('0', f.inputDigit('0'));399 assertEquals('08', f.inputDigit('8'));400 assertEquals('080', f.inputDigit('0'));401 assertEquals('080 7', f.inputDigit('7'));402 assertEquals('080 70', f.inputDigit('0'));403 assertEquals('080 703', f.inputDigit('3'));404 assertEquals('080 7031', f.inputDigit('1'));405 assertEquals('080 7031 3', f.inputDigit('3'));406 assertEquals('080 7031 30', f.inputDigit('0'));407 assertEquals('080 7031 300', f.inputDigit('0'));408 assertEquals('080 7031 3000', f.inputDigit('0'));409}410function testAYTFGBPremiumRate() {411 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */412 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.GB);413 assertEquals('0', f.inputDigit('0'));414 assertEquals('09', f.inputDigit('9'));415 assertEquals('090', f.inputDigit('0'));416 assertEquals('090 7', f.inputDigit('7'));417 assertEquals('090 70', f.inputDigit('0'));418 assertEquals('090 703', f.inputDigit('3'));419 assertEquals('090 7031', f.inputDigit('1'));420 assertEquals('090 7031 3', f.inputDigit('3'));421 assertEquals('090 7031 30', f.inputDigit('0'));422 assertEquals('090 7031 300', f.inputDigit('0'));423 assertEquals('090 7031 3000', f.inputDigit('0'));424}425function testAYTFNZMobile() {426 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */427 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.NZ);428 assertEquals('0', f.inputDigit('0'));429 assertEquals('02', f.inputDigit('2'));430 assertEquals('021', f.inputDigit('1'));431 assertEquals('02-11', f.inputDigit('1'));432 assertEquals('02-112', f.inputDigit('2'));433 // Note the unittest is using fake metadata which might produce non-ideal434 // results.435 assertEquals('02-112 3', f.inputDigit('3'));436 assertEquals('02-112 34', f.inputDigit('4'));437 assertEquals('02-112 345', f.inputDigit('5'));438 assertEquals('02-112 3456', f.inputDigit('6'));439}440function testAYTFDE() {441 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */442 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.DE);443 assertEquals('0', f.inputDigit('0'));444 assertEquals('03', f.inputDigit('3'));445 assertEquals('030', f.inputDigit('0'));446 assertEquals('030/1', f.inputDigit('1'));447 assertEquals('030/12', f.inputDigit('2'));448 assertEquals('030/123', f.inputDigit('3'));449 assertEquals('030/1234', f.inputDigit('4'));450 // 04134 1234451 f.clear();452 assertEquals('0', f.inputDigit('0'));453 assertEquals('04', f.inputDigit('4'));454 assertEquals('041', f.inputDigit('1'));455 assertEquals('041 3', f.inputDigit('3'));456 assertEquals('041 34', f.inputDigit('4'));457 assertEquals('04134 1', f.inputDigit('1'));458 assertEquals('04134 12', f.inputDigit('2'));459 assertEquals('04134 123', f.inputDigit('3'));460 assertEquals('04134 1234', f.inputDigit('4'));461 // 08021 2345462 f.clear();463 assertEquals('0', f.inputDigit('0'));464 assertEquals('08', f.inputDigit('8'));465 assertEquals('080', f.inputDigit('0'));466 assertEquals('080 2', f.inputDigit('2'));467 assertEquals('080 21', f.inputDigit('1'));468 assertEquals('08021 2', f.inputDigit('2'));469 assertEquals('08021 23', f.inputDigit('3'));470 assertEquals('08021 234', f.inputDigit('4'));471 assertEquals('08021 2345', f.inputDigit('5'));472 // 00 1 650 253 2250473 f.clear();474 assertEquals('0', f.inputDigit('0'));475 assertEquals('00', f.inputDigit('0'));476 assertEquals('00 1 ', f.inputDigit('1'));477 assertEquals('00 1 6', f.inputDigit('6'));478 assertEquals('00 1 65', f.inputDigit('5'));479 assertEquals('00 1 650', f.inputDigit('0'));480 assertEquals('00 1 650 2', f.inputDigit('2'));481 assertEquals('00 1 650 25', f.inputDigit('5'));482 assertEquals('00 1 650 253', f.inputDigit('3'));483 assertEquals('00 1 650 253 2', f.inputDigit('2'));484 assertEquals('00 1 650 253 22', f.inputDigit('2'));485 assertEquals('00 1 650 253 222', f.inputDigit('2'));486 assertEquals('00 1 650 253 2222', f.inputDigit('2'));487}488function testAYTFAR() {489 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */490 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.AR);491 assertEquals('0', f.inputDigit('0'));492 assertEquals('01', f.inputDigit('1'));493 assertEquals('011', f.inputDigit('1'));494 assertEquals('011 7', f.inputDigit('7'));495 assertEquals('011 70', f.inputDigit('0'));496 assertEquals('011 703', f.inputDigit('3'));497 assertEquals('011 7031', f.inputDigit('1'));498 assertEquals('011 7031-3', f.inputDigit('3'));499 assertEquals('011 7031-30', f.inputDigit('0'));500 assertEquals('011 7031-300', f.inputDigit('0'));501 assertEquals('011 7031-3000', f.inputDigit('0'));502}503function testAYTFARMobile() {504 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */505 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.AR);506 assertEquals('+', f.inputDigit('+'));507 assertEquals('+5', f.inputDigit('5'));508 assertEquals('+54 ', f.inputDigit('4'));509 assertEquals('+54 9', f.inputDigit('9'));510 assertEquals('+54 91', f.inputDigit('1'));511 assertEquals('+54 9 11', f.inputDigit('1'));512 assertEquals('+54 9 11 2', f.inputDigit('2'));513 assertEquals('+54 9 11 23', f.inputDigit('3'));514 assertEquals('+54 9 11 231', f.inputDigit('1'));515 assertEquals('+54 9 11 2312', f.inputDigit('2'));516 assertEquals('+54 9 11 2312 1', f.inputDigit('1'));517 assertEquals('+54 9 11 2312 12', f.inputDigit('2'));518 assertEquals('+54 9 11 2312 123', f.inputDigit('3'));519 assertEquals('+54 9 11 2312 1234', f.inputDigit('4'));520}521function testAYTFKR() {522 // +82 51 234 5678523 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */524 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.KR);525 assertEquals('+', f.inputDigit('+'));526 assertEquals('+8', f.inputDigit('8'));527 assertEquals('+82 ', f.inputDigit('2'));528 assertEquals('+82 5', f.inputDigit('5'));529 assertEquals('+82 51', f.inputDigit('1'));530 assertEquals('+82 51-2', f.inputDigit('2'));531 assertEquals('+82 51-23', f.inputDigit('3'));532 assertEquals('+82 51-234', f.inputDigit('4'));533 assertEquals('+82 51-234-5', f.inputDigit('5'));534 assertEquals('+82 51-234-56', f.inputDigit('6'));535 assertEquals('+82 51-234-567', f.inputDigit('7'));536 assertEquals('+82 51-234-5678', f.inputDigit('8'));537 // +82 2 531 5678538 f.clear();539 assertEquals('+', f.inputDigit('+'));540 assertEquals('+8', f.inputDigit('8'));541 assertEquals('+82 ', f.inputDigit('2'));542 assertEquals('+82 2', f.inputDigit('2'));543 assertEquals('+82 25', f.inputDigit('5'));544 assertEquals('+82 2-53', f.inputDigit('3'));545 assertEquals('+82 2-531', f.inputDigit('1'));546 assertEquals('+82 2-531-5', f.inputDigit('5'));547 assertEquals('+82 2-531-56', f.inputDigit('6'));548 assertEquals('+82 2-531-567', f.inputDigit('7'));549 assertEquals('+82 2-531-5678', f.inputDigit('8'));550 // +82 2 3665 5678551 f.clear();552 assertEquals('+', f.inputDigit('+'));553 assertEquals('+8', f.inputDigit('8'));554 assertEquals('+82 ', f.inputDigit('2'));555 assertEquals('+82 2', f.inputDigit('2'));556 assertEquals('+82 23', f.inputDigit('3'));557 assertEquals('+82 2-36', f.inputDigit('6'));558 assertEquals('+82 2-366', f.inputDigit('6'));559 assertEquals('+82 2-3665', f.inputDigit('5'));560 assertEquals('+82 2-3665-5', f.inputDigit('5'));561 assertEquals('+82 2-3665-56', f.inputDigit('6'));562 assertEquals('+82 2-3665-567', f.inputDigit('7'));563 assertEquals('+82 2-3665-5678', f.inputDigit('8'));564 // 02-114565 f.clear();566 assertEquals('0', f.inputDigit('0'));567 assertEquals('02', f.inputDigit('2'));568 assertEquals('021', f.inputDigit('1'));569 assertEquals('02-11', f.inputDigit('1'));570 assertEquals('02-114', f.inputDigit('4'));571 // 02-1300572 f.clear();573 assertEquals('0', f.inputDigit('0'));574 assertEquals('02', f.inputDigit('2'));575 assertEquals('021', f.inputDigit('1'));576 assertEquals('02-13', f.inputDigit('3'));577 assertEquals('02-130', f.inputDigit('0'));578 assertEquals('02-1300', f.inputDigit('0'));579 // 011-456-7890580 f.clear();581 assertEquals('0', f.inputDigit('0'));582 assertEquals('01', f.inputDigit('1'));583 assertEquals('011', f.inputDigit('1'));584 assertEquals('011-4', f.inputDigit('4'));585 assertEquals('011-45', f.inputDigit('5'));586 assertEquals('011-456', f.inputDigit('6'));587 assertEquals('011-456-7', f.inputDigit('7'));588 assertEquals('011-456-78', f.inputDigit('8'));589 assertEquals('011-456-789', f.inputDigit('9'));590 assertEquals('011-456-7890', f.inputDigit('0'));591 // 011-9876-7890592 f.clear();593 assertEquals('0', f.inputDigit('0'));594 assertEquals('01', f.inputDigit('1'));595 assertEquals('011', f.inputDigit('1'));596 assertEquals('011-9', f.inputDigit('9'));597 assertEquals('011-98', f.inputDigit('8'));598 assertEquals('011-987', f.inputDigit('7'));599 assertEquals('011-9876', f.inputDigit('6'));600 assertEquals('011-9876-7', f.inputDigit('7'));601 assertEquals('011-9876-78', f.inputDigit('8'));602 assertEquals('011-9876-789', f.inputDigit('9'));603 assertEquals('011-9876-7890', f.inputDigit('0'));604}605function testAYTF_MX() {606 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */607 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.MX);608 // +52 800 123 4567609 assertEquals('+', f.inputDigit('+'));610 assertEquals('+5', f.inputDigit('5'));611 assertEquals('+52 ', f.inputDigit('2'));612 assertEquals('+52 8', f.inputDigit('8'));613 assertEquals('+52 80', f.inputDigit('0'));614 assertEquals('+52 800', f.inputDigit('0'));615 assertEquals('+52 800 1', f.inputDigit('1'));616 assertEquals('+52 800 12', f.inputDigit('2'));617 assertEquals('+52 800 123', f.inputDigit('3'));618 assertEquals('+52 800 123 4', f.inputDigit('4'));619 assertEquals('+52 800 123 45', f.inputDigit('5'));620 assertEquals('+52 800 123 456', f.inputDigit('6'));621 assertEquals('+52 800 123 4567', f.inputDigit('7'));622 // +52 55 1234 5678623 f.clear();624 assertEquals('+', f.inputDigit('+'));625 assertEquals('+5', f.inputDigit('5'));626 assertEquals('+52 ', f.inputDigit('2'));627 assertEquals('+52 5', f.inputDigit('5'));628 assertEquals('+52 55', f.inputDigit('5'));629 assertEquals('+52 55 1', f.inputDigit('1'));630 assertEquals('+52 55 12', f.inputDigit('2'));631 assertEquals('+52 55 123', f.inputDigit('3'));632 assertEquals('+52 55 1234', f.inputDigit('4'));633 assertEquals('+52 55 1234 5', f.inputDigit('5'));634 assertEquals('+52 55 1234 56', f.inputDigit('6'));635 assertEquals('+52 55 1234 567', f.inputDigit('7'));636 assertEquals('+52 55 1234 5678', f.inputDigit('8'));637 // +52 212 345 6789638 f.clear();639 assertEquals('+', f.inputDigit('+'));640 assertEquals('+5', f.inputDigit('5'));641 assertEquals('+52 ', f.inputDigit('2'));642 assertEquals('+52 2', f.inputDigit('2'));643 assertEquals('+52 21', f.inputDigit('1'));644 assertEquals('+52 212', f.inputDigit('2'));645 assertEquals('+52 212 3', f.inputDigit('3'));646 assertEquals('+52 212 34', f.inputDigit('4'));647 assertEquals('+52 212 345', f.inputDigit('5'));648 assertEquals('+52 212 345 6', f.inputDigit('6'));649 assertEquals('+52 212 345 67', f.inputDigit('7'));650 assertEquals('+52 212 345 678', f.inputDigit('8'));651 assertEquals('+52 212 345 6789', f.inputDigit('9'));652 // +52 1 55 1234 5678653 f.clear();654 assertEquals('+', f.inputDigit('+'));655 assertEquals('+5', f.inputDigit('5'));656 assertEquals('+52 ', f.inputDigit('2'));657 assertEquals('+52 1', f.inputDigit('1'));658 assertEquals('+52 15', f.inputDigit('5'));659 assertEquals('+52 1 55', f.inputDigit('5'));660 assertEquals('+52 1 55 1', f.inputDigit('1'));661 assertEquals('+52 1 55 12', f.inputDigit('2'));662 assertEquals('+52 1 55 123', f.inputDigit('3'));663 assertEquals('+52 1 55 1234', f.inputDigit('4'));664 assertEquals('+52 1 55 1234 5', f.inputDigit('5'));665 assertEquals('+52 1 55 1234 56', f.inputDigit('6'));666 assertEquals('+52 1 55 1234 567', f.inputDigit('7'));667 assertEquals('+52 1 55 1234 5678', f.inputDigit('8'));668 // +52 1 541 234 5678669 f.clear();670 assertEquals('+', f.inputDigit('+'));671 assertEquals('+5', f.inputDigit('5'));672 assertEquals('+52 ', f.inputDigit('2'));673 assertEquals('+52 1', f.inputDigit('1'));674 assertEquals('+52 15', f.inputDigit('5'));675 assertEquals('+52 1 54', f.inputDigit('4'));676 assertEquals('+52 1 541', f.inputDigit('1'));677 assertEquals('+52 1 541 2', f.inputDigit('2'));678 assertEquals('+52 1 541 23', f.inputDigit('3'));679 assertEquals('+52 1 541 234', f.inputDigit('4'));680 assertEquals('+52 1 541 234 5', f.inputDigit('5'));681 assertEquals('+52 1 541 234 56', f.inputDigit('6'));682 assertEquals('+52 1 541 234 567', f.inputDigit('7'));683 assertEquals('+52 1 541 234 5678', f.inputDigit('8'));684}685function testAYTF_International_Toll_Free() {686 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */687 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.US);688 // +800 1234 5678689 assertEquals('+', f.inputDigit('+'));690 assertEquals('+8', f.inputDigit('8'));691 assertEquals('+80', f.inputDigit('0'));692 assertEquals('+800 ', f.inputDigit('0'));693 assertEquals('+800 1', f.inputDigit('1'));694 assertEquals('+800 12', f.inputDigit('2'));695 assertEquals('+800 123', f.inputDigit('3'));696 assertEquals('+800 1234', f.inputDigit('4'));697 assertEquals('+800 1234 5', f.inputDigit('5'));698 assertEquals('+800 1234 56', f.inputDigit('6'));699 assertEquals('+800 1234 567', f.inputDigit('7'));700 assertEquals('+800 1234 5678', f.inputDigit('8'));701 assertEquals('+800123456789', f.inputDigit('9'));702}703function testAYTFMultipleLeadingDigitPatterns() {704 // +81 50 2345 6789705 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */706 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.JP);707 assertEquals('+', f.inputDigit('+'));708 assertEquals('+8', f.inputDigit('8'));709 assertEquals('+81 ', f.inputDigit('1'));710 assertEquals('+81 5', f.inputDigit('5'));711 assertEquals('+81 50', f.inputDigit('0'));712 assertEquals('+81 50 2', f.inputDigit('2'));713 assertEquals('+81 50 23', f.inputDigit('3'));714 assertEquals('+81 50 234', f.inputDigit('4'));715 assertEquals('+81 50 2345', f.inputDigit('5'));716 assertEquals('+81 50 2345 6', f.inputDigit('6'));717 assertEquals('+81 50 2345 67', f.inputDigit('7'));718 assertEquals('+81 50 2345 678', f.inputDigit('8'));719 assertEquals('+81 50 2345 6789', f.inputDigit('9'));720 // +81 222 12 5678721 f.clear();722 assertEquals('+', f.inputDigit('+'));723 assertEquals('+8', f.inputDigit('8'));724 assertEquals('+81 ', f.inputDigit('1'));725 assertEquals('+81 2', f.inputDigit('2'));726 assertEquals('+81 22', f.inputDigit('2'));727 assertEquals('+81 22 2', f.inputDigit('2'));728 assertEquals('+81 22 21', f.inputDigit('1'));729 assertEquals('+81 2221 2', f.inputDigit('2'));730 assertEquals('+81 222 12 5', f.inputDigit('5'));731 assertEquals('+81 222 12 56', f.inputDigit('6'));732 assertEquals('+81 222 12 567', f.inputDigit('7'));733 assertEquals('+81 222 12 5678', f.inputDigit('8'));734 // 011113735 f.clear();736 assertEquals('0', f.inputDigit('0'));737 assertEquals('01', f.inputDigit('1'));738 assertEquals('011', f.inputDigit('1'));739 assertEquals('011 1', f.inputDigit('1'));740 assertEquals('011 11', f.inputDigit('1'));741 assertEquals('011113', f.inputDigit('3'));742 // +81 3332 2 5678743 f.clear();744 assertEquals('+', f.inputDigit('+'));745 assertEquals('+8', f.inputDigit('8'));746 assertEquals('+81 ', f.inputDigit('1'));747 assertEquals('+81 3', f.inputDigit('3'));748 assertEquals('+81 33', f.inputDigit('3'));749 assertEquals('+81 33 3', f.inputDigit('3'));750 assertEquals('+81 3332', f.inputDigit('2'));751 assertEquals('+81 3332 2', f.inputDigit('2'));752 assertEquals('+81 3332 2 5', f.inputDigit('5'));753 assertEquals('+81 3332 2 56', f.inputDigit('6'));754 assertEquals('+81 3332 2 567', f.inputDigit('7'));755 assertEquals('+81 3332 2 5678', f.inputDigit('8'));756}757function testAYTFLongIDD_AU() {758 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */759 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.AU);760 // 0011 1 650 253 2250761 assertEquals('0', f.inputDigit('0'));762 assertEquals('00', f.inputDigit('0'));763 assertEquals('001', f.inputDigit('1'));764 assertEquals('0011', f.inputDigit('1'));765 assertEquals('0011 1 ', f.inputDigit('1'));766 assertEquals('0011 1 6', f.inputDigit('6'));767 assertEquals('0011 1 65', f.inputDigit('5'));768 assertEquals('0011 1 650', f.inputDigit('0'));769 assertEquals('0011 1 650 2', f.inputDigit('2'));770 assertEquals('0011 1 650 25', f.inputDigit('5'));771 assertEquals('0011 1 650 253', f.inputDigit('3'));772 assertEquals('0011 1 650 253 2', f.inputDigit('2'));773 assertEquals('0011 1 650 253 22', f.inputDigit('2'));774 assertEquals('0011 1 650 253 222', f.inputDigit('2'));775 assertEquals('0011 1 650 253 2222', f.inputDigit('2'));776 // 0011 81 3332 2 5678777 f.clear();778 assertEquals('0', f.inputDigit('0'));779 assertEquals('00', f.inputDigit('0'));780 assertEquals('001', f.inputDigit('1'));781 assertEquals('0011', f.inputDigit('1'));782 assertEquals('00118', f.inputDigit('8'));783 assertEquals('0011 81 ', f.inputDigit('1'));784 assertEquals('0011 81 3', f.inputDigit('3'));785 assertEquals('0011 81 33', f.inputDigit('3'));786 assertEquals('0011 81 33 3', f.inputDigit('3'));787 assertEquals('0011 81 3332', f.inputDigit('2'));788 assertEquals('0011 81 3332 2', f.inputDigit('2'));789 assertEquals('0011 81 3332 2 5', f.inputDigit('5'));790 assertEquals('0011 81 3332 2 56', f.inputDigit('6'));791 assertEquals('0011 81 3332 2 567', f.inputDigit('7'));792 assertEquals('0011 81 3332 2 5678', f.inputDigit('8'));793 // 0011 244 250 253 222794 f.clear();795 assertEquals('0', f.inputDigit('0'));796 assertEquals('00', f.inputDigit('0'));797 assertEquals('001', f.inputDigit('1'));798 assertEquals('0011', f.inputDigit('1'));799 assertEquals('00112', f.inputDigit('2'));800 assertEquals('001124', f.inputDigit('4'));801 assertEquals('0011 244 ', f.inputDigit('4'));802 assertEquals('0011 244 2', f.inputDigit('2'));803 assertEquals('0011 244 25', f.inputDigit('5'));804 assertEquals('0011 244 250', f.inputDigit('0'));805 assertEquals('0011 244 250 2', f.inputDigit('2'));806 assertEquals('0011 244 250 25', f.inputDigit('5'));807 assertEquals('0011 244 250 253', f.inputDigit('3'));808 assertEquals('0011 244 250 253 2', f.inputDigit('2'));809 assertEquals('0011 244 250 253 22', f.inputDigit('2'));810 assertEquals('0011 244 250 253 222', f.inputDigit('2'));811}812function testAYTFLongIDD_KR() {813 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */814 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.KR);815 // 00300 1 650 253 2222816 assertEquals('0', f.inputDigit('0'));817 assertEquals('00', f.inputDigit('0'));818 assertEquals('003', f.inputDigit('3'));819 assertEquals('0030', f.inputDigit('0'));820 assertEquals('00300', f.inputDigit('0'));821 assertEquals('00300 1 ', f.inputDigit('1'));822 assertEquals('00300 1 6', f.inputDigit('6'));823 assertEquals('00300 1 65', f.inputDigit('5'));824 assertEquals('00300 1 650', f.inputDigit('0'));825 assertEquals('00300 1 650 2', f.inputDigit('2'));826 assertEquals('00300 1 650 25', f.inputDigit('5'));827 assertEquals('00300 1 650 253', f.inputDigit('3'));828 assertEquals('00300 1 650 253 2', f.inputDigit('2'));829 assertEquals('00300 1 650 253 22', f.inputDigit('2'));830 assertEquals('00300 1 650 253 222', f.inputDigit('2'));831 assertEquals('00300 1 650 253 2222', f.inputDigit('2'));832}833function testAYTFLongNDD_KR() {834 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */835 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.KR);836 // 08811-9876-7890837 assertEquals('0', f.inputDigit('0'));838 assertEquals('08', f.inputDigit('8'));839 assertEquals('088', f.inputDigit('8'));840 assertEquals('0881', f.inputDigit('1'));841 assertEquals('08811', f.inputDigit('1'));842 assertEquals('08811-9', f.inputDigit('9'));843 assertEquals('08811-98', f.inputDigit('8'));844 assertEquals('08811-987', f.inputDigit('7'));845 assertEquals('08811-9876', f.inputDigit('6'));846 assertEquals('08811-9876-7', f.inputDigit('7'));847 assertEquals('08811-9876-78', f.inputDigit('8'));848 assertEquals('08811-9876-789', f.inputDigit('9'));849 assertEquals('08811-9876-7890', f.inputDigit('0'));850 // 08500 11-9876-7890851 f.clear();852 assertEquals('0', f.inputDigit('0'));853 assertEquals('08', f.inputDigit('8'));854 assertEquals('085', f.inputDigit('5'));855 assertEquals('0850', f.inputDigit('0'));856 assertEquals('08500 ', f.inputDigit('0'));857 assertEquals('08500 1', f.inputDigit('1'));858 assertEquals('08500 11', f.inputDigit('1'));859 assertEquals('08500 11-9', f.inputDigit('9'));860 assertEquals('08500 11-98', f.inputDigit('8'));861 assertEquals('08500 11-987', f.inputDigit('7'));862 assertEquals('08500 11-9876', f.inputDigit('6'));863 assertEquals('08500 11-9876-7', f.inputDigit('7'));864 assertEquals('08500 11-9876-78', f.inputDigit('8'));865 assertEquals('08500 11-9876-789', f.inputDigit('9'));866 assertEquals('08500 11-9876-7890', f.inputDigit('0'));867}868function testAYTFLongNDD_SG() {869 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */870 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.SG);871 // 777777 9876 7890872 assertEquals('7', f.inputDigit('7'));873 assertEquals('77', f.inputDigit('7'));874 assertEquals('777', f.inputDigit('7'));875 assertEquals('7777', f.inputDigit('7'));876 assertEquals('77777', f.inputDigit('7'));877 assertEquals('777777 ', f.inputDigit('7'));878 assertEquals('777777 9', f.inputDigit('9'));879 assertEquals('777777 98', f.inputDigit('8'));880 assertEquals('777777 987', f.inputDigit('7'));881 assertEquals('777777 9876', f.inputDigit('6'));882 assertEquals('777777 9876 7', f.inputDigit('7'));883 assertEquals('777777 9876 78', f.inputDigit('8'));884 assertEquals('777777 9876 789', f.inputDigit('9'));885 assertEquals('777777 9876 7890', f.inputDigit('0'));886}887function testAYTFShortNumberFormattingFix_AU() {888 // For Australia, the national prefix is not optional when formatting.889 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */890 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.AU);891 // 1234567890 - For leading digit 1, the national prefix formatting rule has892 // first group only.893 assertEquals('1', f.inputDigit('1'));894 assertEquals('12', f.inputDigit('2'));895 assertEquals('123', f.inputDigit('3'));896 assertEquals('1234', f.inputDigit('4'));897 assertEquals('1234 5', f.inputDigit('5'));898 assertEquals('1234 56', f.inputDigit('6'));899 assertEquals('1234 567', f.inputDigit('7'));900 assertEquals('1234 567 8', f.inputDigit('8'));901 assertEquals('1234 567 89', f.inputDigit('9'));902 assertEquals('1234 567 890', f.inputDigit('0'));903 // +61 1234 567 890 - Test the same number, but with the country code.904 f.clear();905 assertEquals('+', f.inputDigit('+'));906 assertEquals('+6', f.inputDigit('6'));907 assertEquals('+61 ', f.inputDigit('1'));908 assertEquals('+61 1', f.inputDigit('1'));909 assertEquals('+61 12', f.inputDigit('2'));910 assertEquals('+61 123', f.inputDigit('3'));911 assertEquals('+61 1234', f.inputDigit('4'));912 assertEquals('+61 1234 5', f.inputDigit('5'));913 assertEquals('+61 1234 56', f.inputDigit('6'));914 assertEquals('+61 1234 567', f.inputDigit('7'));915 assertEquals('+61 1234 567 8', f.inputDigit('8'));916 assertEquals('+61 1234 567 89', f.inputDigit('9'));917 assertEquals('+61 1234 567 890', f.inputDigit('0'));918 // 212345678 - For leading digit 2, the national prefix formatting rule puts919 // the national prefix before the first group.920 f.clear();921 assertEquals('0', f.inputDigit('0'));922 assertEquals('02', f.inputDigit('2'));923 assertEquals('021', f.inputDigit('1'));924 assertEquals('02 12', f.inputDigit('2'));925 assertEquals('02 123', f.inputDigit('3'));926 assertEquals('02 1234', f.inputDigit('4'));927 assertEquals('02 1234 5', f.inputDigit('5'));928 assertEquals('02 1234 56', f.inputDigit('6'));929 assertEquals('02 1234 567', f.inputDigit('7'));930 assertEquals('02 1234 5678', f.inputDigit('8'));931 // 212345678 - Test the same number, but without the leading 0.932 f.clear();933 assertEquals('2', f.inputDigit('2'));934 assertEquals('21', f.inputDigit('1'));935 assertEquals('212', f.inputDigit('2'));936 assertEquals('2123', f.inputDigit('3'));937 assertEquals('21234', f.inputDigit('4'));938 assertEquals('212345', f.inputDigit('5'));939 assertEquals('2123456', f.inputDigit('6'));940 assertEquals('21234567', f.inputDigit('7'));941 assertEquals('212345678', f.inputDigit('8'));942 // +61 2 1234 5678 - Test the same number, but with the country code.943 f.clear();944 assertEquals('+', f.inputDigit('+'));945 assertEquals('+6', f.inputDigit('6'));946 assertEquals('+61 ', f.inputDigit('1'));947 assertEquals('+61 2', f.inputDigit('2'));948 assertEquals('+61 21', f.inputDigit('1'));949 assertEquals('+61 2 12', f.inputDigit('2'));950 assertEquals('+61 2 123', f.inputDigit('3'));951 assertEquals('+61 2 1234', f.inputDigit('4'));952 assertEquals('+61 2 1234 5', f.inputDigit('5'));953 assertEquals('+61 2 1234 56', f.inputDigit('6'));954 assertEquals('+61 2 1234 567', f.inputDigit('7'));955 assertEquals('+61 2 1234 5678', f.inputDigit('8'));956}957function testAYTFShortNumberFormattingFix_KR() {958 // For Korea, the national prefix is not optional when formatting, and the959 // national prefix formatting rule doesn't consist of only the first group.960 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */961 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.KR);962 // 111963 assertEquals('1', f.inputDigit('1'));964 assertEquals('11', f.inputDigit('1'));965 assertEquals('111', f.inputDigit('1'));966 // 114967 f.clear();968 assertEquals('1', f.inputDigit('1'));969 assertEquals('11', f.inputDigit('1'));970 assertEquals('114', f.inputDigit('4'));971 // 13121234 - Test a mobile number without the national prefix. Even though it972 // is not an emergency number, it should be formatted as a block.973 f.clear();974 assertEquals('1', f.inputDigit('1'));975 assertEquals('13', f.inputDigit('3'));976 assertEquals('131', f.inputDigit('1'));977 assertEquals('1312', f.inputDigit('2'));978 assertEquals('13121', f.inputDigit('1'));979 assertEquals('131212', f.inputDigit('2'));980 assertEquals('1312123', f.inputDigit('3'));981 assertEquals('13121234', f.inputDigit('4'));982 // +82 131-2-1234 - Test the same number, but with the country code.983 f.clear();984 assertEquals('+', f.inputDigit('+'));985 assertEquals('+8', f.inputDigit('8'));986 assertEquals('+82 ', f.inputDigit('2'));987 assertEquals('+82 1', f.inputDigit('1'));988 assertEquals('+82 13', f.inputDigit('3'));989 assertEquals('+82 131', f.inputDigit('1'));990 assertEquals('+82 131-2', f.inputDigit('2'));991 assertEquals('+82 131-2-1', f.inputDigit('1'));992 assertEquals('+82 131-2-12', f.inputDigit('2'));993 assertEquals('+82 131-2-123', f.inputDigit('3'));994 assertEquals('+82 131-2-1234', f.inputDigit('4'));995}996function testAYTFShortNumberFormattingFix_MX() {997 // For Mexico, the national prefix is optional when formatting.998 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.MX);999 // 9111000 assertEquals('9', f.inputDigit('9'));1001 assertEquals('91', f.inputDigit('1'));1002 assertEquals('911', f.inputDigit('1'));1003 // 800 123 4567 - Test a toll-free number, which should have a formatting rule1004 // applied to it even though it doesn't begin with the national prefix.1005 f.clear();1006 assertEquals('8', f.inputDigit('8'));1007 assertEquals('80', f.inputDigit('0'));1008 assertEquals('800', f.inputDigit('0'));1009 assertEquals('800 1', f.inputDigit('1'));1010 assertEquals('800 12', f.inputDigit('2'));1011 assertEquals('800 123', f.inputDigit('3'));1012 assertEquals('800 123 4', f.inputDigit('4'));1013 assertEquals('800 123 45', f.inputDigit('5'));1014 assertEquals('800 123 456', f.inputDigit('6'));1015 assertEquals('800 123 4567', f.inputDigit('7'));1016 // +52 800 123 4567 - Test the same number, but with the country code.1017 f.clear();1018 assertEquals('+', f.inputDigit('+'));1019 assertEquals('+5', f.inputDigit('5'));1020 assertEquals('+52 ', f.inputDigit('2'));1021 assertEquals('+52 8', f.inputDigit('8'));1022 assertEquals('+52 80', f.inputDigit('0'));1023 assertEquals('+52 800', f.inputDigit('0'));1024 assertEquals('+52 800 1', f.inputDigit('1'));1025 assertEquals('+52 800 12', f.inputDigit('2'));1026 assertEquals('+52 800 123', f.inputDigit('3'));1027 assertEquals('+52 800 123 4', f.inputDigit('4'));1028 assertEquals('+52 800 123 45', f.inputDigit('5'));1029 assertEquals('+52 800 123 456', f.inputDigit('6'));1030 assertEquals('+52 800 123 4567', f.inputDigit('7'));1031}1032function testAYTFNoNationalPrefix() {1033 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */1034 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.IT);1035 assertEquals('3', f.inputDigit('3'));1036 assertEquals('33', f.inputDigit('3'));1037 assertEquals('333', f.inputDigit('3'));1038 assertEquals('333 3', f.inputDigit('3'));1039 assertEquals('333 33', f.inputDigit('3'));1040 assertEquals('333 333', f.inputDigit('3'));1041}1042function testAYTFNoNationalPrefixFormattingRule() {1043 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */1044 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.AO);1045 assertEquals('3', f.inputDigit('3'));1046 assertEquals('33', f.inputDigit('3'));1047 assertEquals('333', f.inputDigit('3'));1048 assertEquals('333 3', f.inputDigit('3'));1049 assertEquals('333 33', f.inputDigit('3'));1050 assertEquals('333 333', f.inputDigit('3'));1051}1052function testAYTFShortNumberFormattingFix_US() {1053 // For the US, an initial 1 is treated specially.1054 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */1055 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.US);1056 // 101 - Test that the initial 1 is not treated as a national prefix.1057 assertEquals('1', f.inputDigit('1'));1058 assertEquals('10', f.inputDigit('0'));1059 assertEquals('101', f.inputDigit('1'));1060 // 112 - Test that the initial 1 is not treated as a national prefix.1061 f.clear();1062 assertEquals('1', f.inputDigit('1'));1063 assertEquals('11', f.inputDigit('1'));1064 assertEquals('112', f.inputDigit('2'));1065 // 122 - Test that the initial 1 is treated as a national prefix.1066 f.clear();1067 assertEquals('1', f.inputDigit('1'));1068 assertEquals('12', f.inputDigit('2'));1069 assertEquals('1 22', f.inputDigit('2'));1070}1071function testAYTFClearNDDAfterIddExtraction() {1072 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */1073 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.KR);1074 assertEquals('0', f.inputDigit('0'));1075 assertEquals('00', f.inputDigit('0'));1076 assertEquals('007', f.inputDigit('7'));1077 assertEquals('0070', f.inputDigit('0'));1078 assertEquals('00700', f.inputDigit('0'));1079 // NDD is '0' at this stage (the first '0' in '00700') because it's not1080 // clear if the number is a national number or using the IDD to dial out.1081 assertEquals('00700 1 ', f.inputDigit('1'));1082 // NDD should be cleared here because IDD '00700' was extracted after the1083 // country calling code '1' (US) was entered.1084 assertEquals('00700 1 2', f.inputDigit('2'));1085 // The remaining long sequence of inputs is because the original bug that1086 // this test if for only triggered after a lot of subsequent inputs.1087 assertEquals('00700 1 23', f.inputDigit('3'));1088 assertEquals('00700 1 234', f.inputDigit('4'));1089 assertEquals('00700 1 234 5', f.inputDigit('5'));1090 assertEquals('00700 1 234 56', f.inputDigit('6'));1091 assertEquals('00700 1 234 567', f.inputDigit('7'));1092 assertEquals('00700 1 234 567 8', f.inputDigit('8'));1093 assertEquals('00700 1 234 567 89', f.inputDigit('9'));1094 assertEquals('00700 1 234 567 890', f.inputDigit('0'));1095 assertEquals('00700 1 234 567 8901', f.inputDigit('1'));1096 assertEquals('00700123456789012', f.inputDigit('2'));1097 assertEquals('007001234567890123', f.inputDigit('3'));1098 assertEquals('0070012345678901234', f.inputDigit('4'));1099 assertEquals('00700123456789012345', f.inputDigit('5'));1100 assertEquals('007001234567890123456', f.inputDigit('6'));1101 assertEquals('0070012345678901234567', f.inputDigit('7'));1102}1103function testAYTFNumberPatternsBecomingInvalidShouldNotResultInDigitLoss() {1104 /** @type {i18n.phonenumbers.AsYouTypeFormatter} */1105 var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.CN);1106 assertEquals('+', f.inputDigit('+'));1107 assertEquals('+8', f.inputDigit('8'));1108 assertEquals('+86 ', f.inputDigit('6'));1109 assertEquals('+86 9', f.inputDigit('9'));1110 assertEquals('+86 98', f.inputDigit('8'));1111 assertEquals('+86 988', f.inputDigit('8'));1112 assertEquals('+86 988 1', f.inputDigit('1'));1113 // Now the number pattern is no longer valid because there are multiple1114 // leading digit patterns; when we try again to extract a country code we1115 // should ensure we use the last leading digit pattern, rather than the first1116 // one such that it *thinks* it's found a valid formatting rule again.1117 // https://github.com/google/libphonenumber/issues/4371118 assertEquals('+8698812', f.inputDigit('2'));1119 assertEquals('+86988123', f.inputDigit('3'));1120 assertEquals('+869881234', f.inputDigit('4'));1121 assertEquals('+8698812345', f.inputDigit('5'));...

Full Screen

Full Screen

jsvat.js

Source:jsvat.js Github

copy

Full Screen

1/*==================================================================2Application: Utility Function3Author: John Gardner4Website: http://www.braemoor.co.uk/software/vat.shtml5Version: V1.06Date: 30th July 20057Description: Used to check the validity of an EU country VAT number8Version: V1.19Date: 3rd August 200510Description: Lithuanian legal entities & Maltese check digit checks added.11Version: V1.212Date: 20th October 200513Description: Italian checks refined (thanks Matteo Mike Peluso).14Version: V1.315Date: 16th November 200516Description: Error in GB numbers ending in 00 fixed (thanks Guy Dawson).17Version: V1.418Date: 28th September 200619Description: EU-type numbers added.20Version: V1.521Date: 1st January 200722Description: Romanian and Bulgarian numbers added.23Version: V1.624Date: 7th January 200725Description: Error with Slovenian numbers (thanks to Ales Hotko).26Version: V1.727Date: 10th February 200728Description: Romanian check digits added.29 Thanks to Dragu Costel for the test suite.30Version: V1.831Date: 3rd August 200732Description: IE code modified to allow + and * in old format numbers.33 Thanks to Antonin Moy of Sphere Solutions for pointing out the error.34Version: V1.935Date: 6th August 200736Description: BE code modified to make a specific check that the leading character of 10 digit37 numbers is 0 (belts and braces).38Version: V1.1039Date: 10th August 200740Description: Cypriot check digit support added.41 Check digit validation support for non-standard UK numbers42Version: V1.1143Date: 25th September 200744Description: Spain check digit support for personal numbers.45 Author: David Perez Carmona46Version: V1.1247Date: 23rd November 200948Description: GB code modified to take into account new style check digits.49 Thanks to Guy Dawson of Crossflight Ltd for pointing out the necessity.50Version: V1.1351Date: 7th July 201252Description: EL, GB, SE and BE formats updated - thanks to Joost Van Biervliet of VAT Applications53Version: V.1454Date: 8th April 201355Description: BE Pattern match refined56 BG Add check digit checks for all four types of VAT number57 CY Pattern match improved58 CZ Personal pattern match checking improved59 CZ Personal check digits incorporated60 EE improved pattern match61 ES Physical person number checking refined62 GB Check digit support provided for 12 digit VAT codes and range checks included63 IT Bug removed to allow 999 and 888 issuing office codes64 LT temporarily registered taxpayers check digit support added65 LV Natural persons checks added66 RO improved pattern match67 SK improved pattern match and added check digit support68 Thanks to Theo Vroom for his help in this latest release.69Version: V1.1570Date: 15th April 201371 Swedish algorithm re-implemented.72Version: V1.1673Date: 25th July 201374 Support for Croatian numbers added75Version V1.1776 10th September 201377 Support for Norwegian MVA numbers added (yes, I know that Norway is not in the EU!)78Version V1.1879 29th October 201380 Partial support for new style Irish numbers.81 See http://www.revenue.ie/en/practitioner/ebrief/2013/no-032013.html82 Thanks to Simon Leigh for drawing the author's attention to this.83Version V1.1984 31st October 201385 Support for Serbian PBI numbers added (yes, I know that Serbia is not in the EU!)86Version V1.2087 1st November 201388 Support for Swiss MWST numbers added (yes, I know that Switzerland is not in the EU!)89Version V1.2190 16th December 201491 Non-critical code tidies to French and Danish regular expressions.92 Thanks to Bill Seddon of Lyquidity Solutions93Version V1.2294 14th January 201495 Non-critical code tidy to regular expression for new format Irish numbers.96 Thanks to Olivier Reubens of UNIT4 C-Logic N.V.97Version V1.2398 10th April 201499 Support for Russian INN numbers added (yes, I know that Russia is not in the EU!).100 Thanks to Marco Cesaratto of Arki Tech, Italy101Version V1.24102 4th June 2014103 Check digit validation supported for Irish Type 3 numbers104 Thanks to Olivier Reubens of UNIT4 C-Logic N.V.105Version V1.25106 29th July 2014107 Code improvements108 Thanks to Sébastien Boelpaep and Nate Kerkhofs109Version V1.26110 4th May 2015111 Code improvements to regular expressions112 Thanks to Robert Gust-Bardon of webcraft.ch113Version V1.27114 3rd December 2015115 Extend Swiss optional suffix to allow TVA and ITA116 Thanks to Oskars Petermanis117Version V1.28118 30th August 2016119 Correct Swiss optional suffix to allow TVA and IVA120 Thanks to Jan Verhaegen121Version V1.29122 29th July 2017123 Correct Czeck Republic checking of Individual type 2 - Special Cases124 Thanks to Andreas Wuermser of Auer Packaging UK125Parameters: toCheck - VAT number be checked.126This function checks the value of the parameter for a valid European VAT number.127If the number is found to be invalid format, the function returns a value of false. Otherwise it128returns the VAT number re-formatted.129Example call:130 if (checkVATNumber (myVATNumber))131 alert ("VAT number has a valid format")132 else133 alert ("VAT number has invalid format");134---------------------------------------------------------------------------------------------------*/135var checkVATNumber = (function (){136 // Array holds the regular expressions for the valid VAT number137 var vatexp = new Array();138 // To change the default country (e.g. from the UK to Germany - DE):139 // 1. Change the country code in the defCCode variable below to "DE".140 // 2. Remove the question mark from the regular expressions associated with the UK VAT number:141 // i.e. "(GB)?" -> "(GB)"142 // 3. Add a question mark into the regular expression associated with Germany's number143 // following the country code: i.e. "(DE)" -> "(DE)?"144 var defCCode = "GB";145 // Note - VAT codes without the "**" in the comment do not have check digit checking.146 vatexp.push(/^(AT)U(\d{8})$/); //** Austria147 vatexp.push(/^(BE)(0?\d{9})$/); //** Belgium148 vatexp.push(/^(BG)(\d{9,10})$/); //** Bulgaria149 vatexp.push(/^(CHE)(\d{9})(MWST|TVA|IVA)?$/); //** Switzerland150 vatexp.push(/^(CY)([0-59]\d{7}[A-Z])$/); //** Cyprus151 vatexp.push(/^(CZ)(\d{8,10})(\d{3})?$/); //** Czech Republic152 vatexp.push(/^(DE)([1-9]\d{8})$/); //** Germany153 vatexp.push(/^(DK)(\d{8})$/); //** Denmark154 vatexp.push(/^(EE)(10\d{7})$/); //** Estonia155 vatexp.push(/^(EL)(\d{9})$/); //** Greece156 vatexp.push(/^(ES)([A-Z]\d{8})$/); //** Spain (National juridical entities)157 vatexp.push(/^(ES)([A-HN-SW]\d{7}[A-J])$/); //** Spain (Other juridical entities)158 vatexp.push(/^(ES)([0-9YZ]\d{7}[A-Z])$/); //** Spain (Personal entities type 1)159 vatexp.push(/^(ES)([KLMX]\d{7}[A-Z])$/); //** Spain (Personal entities type 2)160 vatexp.push(/^(EU)(\d{9})$/); //** EU-type161 vatexp.push(/^(FI)(\d{8})$/); //** Finland162 vatexp.push(/^(FR)(\d{11})$/); //** France (1)163 vatexp.push(/^(FR)([A-HJ-NP-Z]\d{10})$/); // France (2)164 vatexp.push(/^(FR)(\d[A-HJ-NP-Z]\d{9})$/); // France (3)165 vatexp.push(/^(FR)([A-HJ-NP-Z]{2}\d{9})$/); // France (4)166 vatexp.push(/^(GB)?(\d{9})$/); //** UK (Standard)167 vatexp.push(/^(GB)?(\d{12})$/); //** UK (Branches)168 vatexp.push(/^(GB)?(GD\d{3})$/); //** UK (Government)169 vatexp.push(/^(GB)?(HA\d{3})$/); //** UK (Health authority)170 vatexp.push(/^(HR)(\d{11})$/); //** Croatia171 vatexp.push(/^(HU)(\d{8})$/); //** Hungary172 vatexp.push(/^(IE)(\d{7}[A-W])$/); //** Ireland (1)173 vatexp.push(/^(IE)([7-9][A-Z\*\+)]\d{5}[A-W])$/); //** Ireland (2)174 vatexp.push(/^(IE)(\d{7}[A-W][AH])$/); //** Ireland (3)175 vatexp.push(/^(IT)(\d{11})$/); //** Italy176 vatexp.push(/^(LV)(\d{11})$/); //** Latvia177 vatexp.push(/^(LT)(\d{9}|\d{12})$/); //** Lithunia178 vatexp.push(/^(LU)(\d{8})$/); //** Luxembourg179 vatexp.push(/^(MT)([1-9]\d{7})$/); //** Malta180 vatexp.push(/^(NL)(\d{9})B\d{2}$/); //** Netherlands181 vatexp.push(/^(NO)(\d{9})$/); //** Norway (not EU)182 vatexp.push(/^(PL)(\d{10})$/); //** Poland183 vatexp.push(/^(PT)(\d{9})$/); //** Portugal184 vatexp.push(/^(RO)([1-9]\d{1,9})$/); //** Romania185 vatexp.push(/^(RU)(\d{10}|\d{12})$/); //** Russia186 vatexp.push(/^(RS)(\d{9})$/); //** Serbia187 vatexp.push(/^(SI)([1-9]\d{7})$/); //** Slovenia188 vatexp.push(/^(SK)([1-9]\d[2346-9]\d{7})$/); //** Slovakia Republic189 vatexp.push(/^(SE)(\d{10}01)$/); //** Sweden190 function checkVATNumber(toCheck) {191 // Load up the string to check192 var VATNumber = toCheck.toUpperCase();193 // Remove spaces etc. from the VAT number to help validation194 VATNumber = VATNumber.replace(/(\s|-|\.)+/g, '');195 // Assume we're not going to find a valid VAT number196 var valid = false;197 // Check the string against the regular expressions for all types of VAT numbers198 for (var i = 0; i < vatexp.length; i++) {199 // Have we recognised the VAT number?200 if (vatexp[i].test(VATNumber)) {201 // Yes - we have202 var cCode = RegExp.$1; // Isolate country code203 var cNumber = RegExp.$2; // Isolate the number204 if (cCode.length == 0) cCode = defCCode; // Set up default country code205 // Call the appropriate country VAT validation routine depending on the country code206 if (eval(cCode + "VATCheckDigit ('" + cNumber + "')")) valid = VATNumber;207 // Having processed the number, we break from the loop208 break;209 }210 }211 // Return with either an error or the reformatted VAT number212 return valid;213 }214 function ATVATCheckDigit(vatnumber) {215 // Checks the check digits of an Austrian VAT number.216 var total = 0;217 var multipliers = [1, 2, 1, 2, 1, 2, 1];218 var temp = 0;219 // Extract the next digit and multiply by the appropriate multiplier.220 for (var i = 0; i < 7; i++) {221 temp = Number(vatnumber.charAt(i)) * multipliers[i];222 if (temp > 9)223 total += Math.floor(temp / 10) + temp % 10;224 else225 total += temp;226 }227 // Establish check digit.228 total = 10 - (total + 4) % 10;229 if (total == 10) total = 0;230 // Compare it with the last character of the VAT number. If it's the same, then it's valid.231 if (total == vatnumber.slice(7, 8))232 return true;233 else234 return false;235 }236 function BEVATCheckDigit(vatnumber) {237 // Checks the check digits of a Belgium VAT number.238 // Nine digit numbers have a 0 inserted at the front.239 if (vatnumber.length == 9) vatnumber = "0" + vatnumber;240 if (vatnumber.slice(1, 2) == 0) return false;241 // Modulus 97 check on last nine digits242 if (97 - vatnumber.slice(0, 8) % 97 == vatnumber.slice(8, 10))243 return true;244 else245 return false;246 }247 function BGVATCheckDigit(vatnumber) {248 var temp, total, multipliers, i;249 // Checks the check digits of a Bulgarian VAT number.250 if (vatnumber.length == 9) {251 // Check the check digit of 9 digit Bulgarian VAT numbers.252 total = 0;253 // First try to calculate the check digit using the first multipliers254 temp = 0;255 for (i = 0; i < 8; i++) temp += Number(vatnumber.charAt(i)) * (i + 1);256 // See if we have a check digit yet257 total = temp % 11;258 if (total != 10) {259 if (total == vatnumber.slice(8))260 return true;261 else262 return false;263 }264 // We got a modulus of 10 before so we have to keep going. Calculate the new check digit using265 // the different multipliers266 temp = 0;267 for (i = 0; i < 8; i++) temp += Number(vatnumber.charAt(i)) * (i + 3);268 // See if we have a check digit yet. If we still have a modulus of 10, set it to 0.269 total = temp % 11;270 if (total == 10) total = 0;271 if (total == vatnumber.slice(8))272 return true;273 else274 return false;275 }276 // 10 digit VAT code - see if it relates to a standard physical person277 if ((/^\d\d[0-5]\d[0-3]\d\d{4}$/).test(vatnumber)) {278 // Check month279 var month = Number(vatnumber.slice(2, 4));280 if ((month > 0 && month < 13) || (month > 20 && month < 33) || (month > 40 && month < 53)) {281 // Extract the next digit and multiply by the counter.282 multipliers = [2, 4, 8, 5, 10, 9, 7, 3, 6];283 total = 0;284 for (i = 0; i < 9; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];285 // Establish check digit.286 total = total % 11;287 if (total == 10) total = 0;288 // Check to see if the check digit given is correct, If not, try next type of person289 if (total == vatnumber.substr(9, 1)) return true;290 }291 }292 // It doesn't relate to a standard physical person - see if it relates to a foreigner.293 // Extract the next digit and multiply by the counter.294 multipliers = [21, 19, 17, 13, 11, 9, 7, 3, 1];295 total = 0;296 for (i = 0; i < 9; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];297 // Check to see if the check digit given is correct, If not, try next type of person298 if (total % 10 == vatnumber.substr(9, 1)) return true;299 // Finally, if not yet identified, see if it conforms to a miscellaneous VAT number300 // Extract the next digit and multiply by the counter.301 multipliers = [4, 3, 2, 7, 6, 5, 4, 3, 2];302 total = 0;303 for (i = 0; i < 9; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];304 // Establish check digit.305 total = 11 - total % 11;306 if (total == 10) return false;307 if (total == 11) total = 0;308 // Check to see if the check digit given is correct, If not, we have an error with the VAT number309 if (total == vatnumber.substr(9, 1))310 return true;311 else312 return false;313 }314 function CHEVATCheckDigit(vatnumber) {315 // Checks the check digits of a Swiss VAT number.316 // Extract the next digit and multiply by the counter.317 var multipliers = [5, 4, 3, 2, 7, 6, 5, 4];318 var total = 0;319 for (var i = 0; i < 8; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];320 // Establish check digit.321 total = 11 - total % 11;322 if (total == 10) return false;323 if (total == 11) total = 0;324 // Check to see if the check digit given is correct, If not, we have an error with the VAT number325 if (total == vatnumber.substr(8, 1))326 return true;327 else328 return false;329 }330 function CYVATCheckDigit(vatnumber) {331 // Checks the check digits of a Cypriot VAT number.332 // Not allowed to start with '12'333 if (Number(vatnumber.slice(0, 2) == 12)) return false;334 // Extract the next digit and multiply by the counter.335 var total = 0;336 for (var i = 0; i < 8; i++) {337 var temp = Number(vatnumber.charAt(i));338 if (i % 2 == 0) {339 switch (temp) {340 case 0:341 temp = 1;342 break;343 case 1:344 temp = 0;345 break;346 case 2:347 temp = 5;348 break;349 case 3:350 temp = 7;351 break;352 case 4:353 temp = 9;354 break;355 default:356 temp = temp * 2 + 3;357 }358 }359 total += temp;360 }361 // Establish check digit using modulus 26, and translate to char. equivalent.362 total = total % 26;363 total = String.fromCharCode(total + 65);364 // Check to see if the check digit given is correct365 if (total == vatnumber.substr(8, 1))366 return true;367 else368 return false;369 }370 function CZVATCheckDigit(vatnumber) {371 // Checks the check digits of a Czech Republic VAT number.372 var total = 0;373 var multipliers = [8, 7, 6, 5, 4, 3, 2];374 var czexp = new Array();375 czexp[0] = (/^\d{8}$/); // 8 digit legal entities376 // Note - my specification says that that the following should have a range of 0-3 in the fourth377 // digit, but the valid number CZ395601439 did not confrm, so a range of 0-9 has been allowed.378 czexp[1] = (/^[0-5][0-9][0|1|5|6][0-9][0-3][0-9]\d{3}$/); // 9 digit individuals379 czexp[2] = (/^6\d{8}$/); // 9 digit individuals (Special cases)380 czexp[3] = (/^\d{2}[0-3|5-8][0-9][0-3][0-9]\d{4}$/); // 10 digit individuals381 var i = 0;382 var a;383 // Legal entities384 if (czexp[0].test(vatnumber)) {385 // Extract the next digit and multiply by the counter.386 for (i = 0; i < 7; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];387 // Establish check digit.388 total = 11 - total % 11;389 if (total == 10) total = 0;390 if (total == 11) total = 1;391 // Compare it with the last character of the VAT number. If it's the same, then it's valid.392 if (total == vatnumber.slice(7, 8))393 return true;394 else395 return false;396 }397 // Individuals type 1 (Standard) - 9 digits without check digit398 else if (czexp[1].test(vatnumber)) {399 if (Number(vatnumber.slice(0, 2)) > 62) return false;400 return true;401 }402 // Individuals type 2 (Special Cases) - 9 digits including check digit403 else if (czexp[2].test(vatnumber)) {404 // Extract the next digit and multiply by the counter.405 for (i = 0; i < 7; i++) total += Number(vatnumber.charAt(i + 1)) * multipliers[i];406 // Establish check digit pointer into lookup table407 if (total % 11 == 0)408 a = total + 11;409 else410 a = Math.ceil(total / 11) * 11;411 var pointer = a - total;412 // Convert calculated check digit according to a lookup table;413 var lookup = [8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8];414 if (lookup[pointer - 1] == vatnumber.slice(8, 9))415 return true;416 else417 return false;418 }419 // Individuals type 3 - 10 digits420 else if (czexp[3].test(vatnumber)) {421 var temp = Number(vatnumber.slice(0, 2)) + Number(vatnumber.slice(2, 4)) + Number(vatnumber.slice(4, 6)) + Number(vatnumber.slice(6, 8)) + Number(vatnumber.slice(8));422 if (temp % 11 == 0 && Number(vatnumber) % 11 == 0)423 return true;424 else425 return false;426 }427 // else error428 return false;429 }430 function DEVATCheckDigit(vatnumber) {431 // Checks the check digits of a German VAT number.432 var product = 10;433 var sum = 0;434 var checkdigit = 0;435 for (var i = 0; i < 8; i++) {436 // Extract the next digit and implement peculiar algorithm!.437 sum = (Number(vatnumber.charAt(i)) + product) % 10;438 if (sum == 0) {439 sum = 10;440 }441 product = (2 * sum) % 11;442 }443 // Establish check digit.444 if (11 - product == 10) {445 checkdigit = 0;446 } else {447 checkdigit = 11 - product;448 }449 // Compare it with the last two characters of the VAT number. If the same, then it is a valid450 // check digit.451 if (checkdigit == vatnumber.slice(8, 9))452 return true;453 else454 return false;455 }456 function DKVATCheckDigit(vatnumber) {457 // Checks the check digits of a Danish VAT number.458 var total = 0;459 var multipliers = [2, 7, 6, 5, 4, 3, 2, 1];460 // Extract the next digit and multiply by the counter.461 for (var i = 0; i < 8; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];462 // Establish check digit.463 total = total % 11;464 // The remainder should be 0 for it to be valid..465 if (total == 0)466 return true;467 else468 return false;469 }470 function EEVATCheckDigit(vatnumber) {471 // Checks the check digits of an Estonian VAT number.472 var total = 0;473 var multipliers = [3, 7, 1, 3, 7, 1, 3, 7];474 // Extract the next digit and multiply by the counter.475 for (var i = 0; i < 8; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];476 // Establish check digits using modulus 10.477 total = 10 - total % 10;478 if (total == 10) total = 0;479 // Compare it with the last character of the VAT number. If it's the same, then it's valid.480 if (total == vatnumber.slice(8, 9))481 return true;482 else483 return false;484 }485 function ELVATCheckDigit(vatnumber) {486 // Checks the check digits of a Greek VAT number.487 var total = 0;488 var multipliers = [256, 128, 64, 32, 16, 8, 4, 2];489 //eight character numbers should be prefixed with an 0.490 if (vatnumber.length == 8) {491 vatnumber = "0" + vatnumber;492 }493 // Extract the next digit and multiply by the counter.494 for (var i = 0; i < 8; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];495 // Establish check digit.496 total = total % 11;497 if (total > 9) {498 total = 0;499 }500 // Compare it with the last character of the VAT number. If it's the same, then it's valid.501 if (total == vatnumber.slice(8, 9))502 return true;503 else504 return false;505 }506 function ESVATCheckDigit(vatnumber) {507 // Checks the check digits of a Spanish VAT number.508 var total = 0;509 var temp = 0;510 var multipliers = [2, 1, 2, 1, 2, 1, 2];511 var esexp = new Array();512 esexp[0] = (/^[A-H|J|U|V]\d{8}$/);513 esexp[1] = (/^[A-H|N-S|W]\d{7}[A-J]$/);514 esexp[2] = (/^[0-9|Y|Z]\d{7}[A-Z]$/);515 esexp[3] = (/^[K|L|M|X]\d{7}[A-Z]$/);516 var i = 0;517 // National juridical entities518 if (esexp[0].test(vatnumber)) {519 // Extract the next digit and multiply by the counter.520 for (i = 0; i < 7; i++) {521 temp = Number(vatnumber.charAt(i + 1)) * multipliers[i];522 if (temp > 9)523 total += Math.floor(temp / 10) + temp % 10;524 else525 total += temp;526 }527 // Now calculate the check digit itself.528 total = 10 - total % 10;529 if (total == 10) {530 total = 0;531 }532 // Compare it with the last character of the VAT number. If it's the same, then it's valid.533 if (total == vatnumber.slice(8, 9))534 return true;535 else536 return false;537 }538 // Juridical entities other than national ones539 else if (esexp[1].test(vatnumber)) {540 // Extract the next digit and multiply by the counter.541 for (i = 0; i < 7; i++) {542 temp = Number(vatnumber.charAt(i + 1)) * multipliers[i];543 if (temp > 9)544 total += Math.floor(temp / 10) + temp % 10;545 else546 total += temp;547 }548 // Now calculate the check digit itself.549 total = 10 - total % 10;550 total = String.fromCharCode(total + 64);551 // Compare it with the last character of the VAT number. If it's the same, then it's valid.552 if (total == vatnumber.slice(8, 9))553 return true;554 else555 return false;556 }557 // Personal number (NIF) (starting with numeric of Y or Z)558 else if (esexp[2].test(vatnumber)) {559 var tempnumber = vatnumber;560 if (tempnumber.substring(0, 1) == 'Y') tempnumber = tempnumber.replace(/Y/, "1");561 if (tempnumber.substring(0, 1) == 'Z') tempnumber = tempnumber.replace(/Z/, "2");562 return tempnumber.charAt(8) == 'TRWAGMYFPDXBNJZSQVHLCKE'.charAt(Number(tempnumber.substring(0, 8)) % 23);563 }564 // Personal number (NIF) (starting with K, L, M, or X)565 else if (esexp[3].test(vatnumber)) {566 return vatnumber.charAt(8) == 'TRWAGMYFPDXBNJZSQVHLCKE'.charAt(Number(vatnumber.substring(1, 8)) % 23);567 }568 else return false;569 }570 function EUVATCheckDigit(vatnumber) {571 // We know little about EU numbers apart from the fact that the first 3 digits represent the572 // country, and that there are nine digits in total.573 return true;574 }575 function FIVATCheckDigit(vatnumber) {576 // Checks the check digits of a Finnish VAT number.577 var total = 0;578 var multipliers = [7, 9, 10, 5, 8, 4, 2];579 // Extract the next digit and multiply by the counter.580 for (var i = 0; i < 7; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];581 // Establish check digit.582 total = 11 - total % 11;583 if (total > 9) {584 total = 0;585 }586 // Compare it with the last character of the VAT number. If it's the same, then it's valid.587 if (total == vatnumber.slice(7, 8))588 return true;589 else590 return false;591 }592 function FRVATCheckDigit(vatnumber) {593 // Checks the check digits of a French VAT number.594 if (!(/^\d{11}$/).test(vatnumber)) return true;595 // Extract the last nine digits as an integer.596 var total = vatnumber.substring(2);597 // Establish check digit.598 total = (total * 100 + 12) % 97;599 // Compare it with the last character of the VAT number. If it's the same, then it's valid.600 if (total == vatnumber.slice(0, 2))601 return true;602 else603 return false;604 }605 function GBVATCheckDigit(vatnumber) {606 // Checks the check digits of a UK VAT number.607 var multipliers = [8, 7, 6, 5, 4, 3, 2];608 // Government departments609 if (vatnumber.substr(0, 2) == 'GD') {610 if (vatnumber.substr(2, 3) < 500)611 return true;612 else613 return false;614 }615 // Health authorities616 if (vatnumber.substr(0, 2) == 'HA') {617 if (vatnumber.substr(2, 3) > 499)618 return true;619 else620 return false;621 }622 // Standard and commercial numbers623 var total = 0;624 // 0 VAT numbers disallowed!625 if (Number(vatnumber.slice(0)) == 0) return false;626 // Check range is OK for modulus 97 calculation627 var no = Number(vatnumber.slice(0, 7));628 // Extract the next digit and multiply by the counter.629 for (var i = 0; i < 7; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];630 // Old numbers use a simple 97 modulus, but new numbers use an adaptation of that (less 55). Our631 // VAT number could use either system, so we check it against both.632 // Establish check digits by subtracting 97 from total until negative.633 var cd = total;634 while (cd > 0) {635 cd = cd - 97;636 }637 // Get the absolute value and compare it with the last two characters of the VAT number. If the638 // same, then it is a valid traditional check digit. However, even then the number must fit within639 // certain specified ranges.640 cd = Math.abs(cd);641 if (cd == vatnumber.slice(7, 9) && no < 9990001 && (no < 100000 || no > 999999) && (no < 9490001 || no > 9700000)) return true;642 // Now try the new method by subtracting 55 from the check digit if we can - else add 42643 if (cd >= 55)644 cd = cd - 55;645 else646 cd = cd + 42;647 if (cd == vatnumber.slice(7, 9) && no > 1000000)648 return true;649 else650 return false;651 }652 function HRVATCheckDigit(vatnumber) {653 // Checks the check digits of a Croatian VAT number using ISO 7064, MOD 11-10 for check digit.654 var product = 10;655 var sum = 0;656 var checkdigit = 0;657 for (var i = 0; i < 10; i++) {658 // Extract the next digit and implement the algorithm659 sum = (Number(vatnumber.charAt(i)) + product) % 10;660 if (sum == 0) {661 sum = 10;662 }663 product = (2 * sum) % 11;664 }665 // Now check that we have the right check digit666 if ((product + vatnumber.slice(10, 11) * 1) % 10 == 1)667 return true;668 else669 return false;670 }671 function HUVATCheckDigit(vatnumber) {672 // Checks the check digits of a Hungarian VAT number.673 var total = 0;674 var multipliers = [9, 7, 3, 1, 9, 7, 3];675 // Extract the next digit and multiply by the counter.676 for (var i = 0; i < 7; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];677 // Establish check digit.678 total = 10 - total % 10;679 if (total == 10) total = 0;680 // Compare it with the last character of the VAT number. If it's the same, then it's valid.681 if (total == vatnumber.slice(7, 8))682 return true;683 else684 return false;685 }686 function IEVATCheckDigit(vatnumber) {687 // Checks the check digits of an Irish VAT number.688 var total = 0;689 var multipliers = [8, 7, 6, 5, 4, 3, 2];690 // If the code is type 1 format, we need to convert it to the new before performing the validation.691 if (/^\d[A-Z\*\+]/.test(vatnumber)) vatnumber = "0" + vatnumber.substring(2, 7) + vatnumber.substring(0, 1) + vatnumber.substring(7, 8);692 // Extract the next digit and multiply by the counter.693 for (var i = 0; i < 7; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];694 // If the number is type 3 then we need to include the trailing A or H in the calculation695 if (/^\d{7}[A-Z][AH]$/.test(vatnumber)) {696 // Add in a multiplier for the character A (1*9=9) or H (8*9=72)697 if (vatnumber.charAt(8) == 'H')698 total += 72;699 else700 total += 9;701 }702 // Establish check digit using modulus 23, and translate to char. equivalent.703 total = total % 23;704 if (total == 0)705 total = "W";706 else707 total = String.fromCharCode(total + 64);708 // Compare it with the eighth character of the VAT number. If it's the same, then it's valid.709 if (total == vatnumber.slice(7, 8))710 return true;711 else712 return false;713 }714 function ITVATCheckDigit(vatnumber) {715 // Checks the check digits of an Italian VAT number.716 var total = 0;717 var multipliers = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2];718 var temp;719 // The last three digits are the issuing office, and cannot exceed more 201, unless 999 or 888720 if (Number(vatnumber.slice(0, 7)) == 0) return false;721 temp = Number(vatnumber.slice(7, 10));722 if ((temp < 1) || (temp > 201) && temp != 999 && temp != 888) return false;723 // Extract the next digit and multiply by the appropriate724 for (var i = 0; i < 10; i++) {725 temp = Number(vatnumber.charAt(i)) * multipliers[i];726 if (temp > 9)727 total += Math.floor(temp / 10) + temp % 10;728 else729 total += temp;730 }731 // Establish check digit.732 total = 10 - total % 10;733 if (total > 9) {734 total = 0;735 }736 // Compare it with the last character of the VAT number. If it's the same, then it's valid.737 if (total == vatnumber.slice(10, 11))738 return true;739 else740 return false;741 }742 function LTVATCheckDigit(vatnumber) {743 // Checks the check digits of a Lithuanian VAT number.744 var total, multipliers, i;745 // 9 character VAT numbers are for legal persons746 if (vatnumber.length == 9) {747 // 8th character must be one748 if (!(/^\d{7}1/).test(vatnumber)) return false;749 // Extract the next digit and multiply by the counter+1.750 total = 0;751 for (i = 0; i < 8; i++) total += Number(vatnumber.charAt(i)) * (i + 1);752 // Can have a double check digit calculation!753 if (total % 11 == 10) {754 multipliers = [3, 4, 5, 6, 7, 8, 9, 1];755 total = 0;756 for (i = 0; i < 8; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];757 }758 // Establish check digit.759 total = total % 11;760 if (total == 10) {761 total = 0;762 }763 // Compare it with the last character of the VAT number. If it's the same, then it's valid.764 if (total == vatnumber.slice(8, 9))765 return true;766 else767 return false;768 }769 // 12 character VAT numbers are for temporarily registered taxpayers770 else {771 // 11th character must be one772 if (!(/^\d{10}1/).test(vatnumber)) return false;773 // Extract the next digit and multiply by the counter+1.774 total = 0;775 multipliers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2];776 for (i = 0; i < 11; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];777 // Can have a double check digit calculation!778 if (total % 11 == 10) {779 multipliers = [3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4];780 total = 0;781 for (i = 0; i < 11; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];782 }783 // Establish check digit.784 total = total % 11;785 if (total == 10) {786 total = 0;787 }788 // Compare it with the last character of the VAT number. If it's the same, then it's valid.789 if (total == vatnumber.slice(11, 12))790 return true;791 else792 return false;793 }794 }795 function LUVATCheckDigit(vatnumber) {796 // Checks the check digits of a Luxembourg VAT number.797 if (vatnumber.slice(0, 6) % 89 == vatnumber.slice(6, 8))798 return true;799 else800 return false;801 }802 function LVVATCheckDigit(vatnumber) {803 // Checks the check digits of a Latvian VAT number.804 // Differentiate between legal entities and natural bodies. For the latter we simply check that805 // the first six digits correspond to valid DDMMYY dates.806 if ((/^[0-3]/).test(vatnumber)) {807 if ((/^[0-3][0-9][0-1][0-9]/).test(vatnumber))808 return true;809 else810 return false;811 }812 else {813 var total = 0;814 var multipliers = [9, 1, 4, 8, 3, 10, 2, 5, 7, 6];815 // Extract the next digit and multiply by the counter.816 for (var i = 0; i < 10; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];817 // Establish check digits by getting modulus 11.818 if (total % 11 == 4 && vatnumber[0] == 9) total = total - 45;819 if (total % 11 == 4)820 total = 4 - total % 11;821 else if (total % 11 > 4)822 total = 14 - total % 11;823 else if (total % 11 < 4)824 total = 3 - total % 11;825 // Compare it with the last character of the VAT number. If it's the same, then it's valid.826 if (total == vatnumber.slice(10, 11))827 return true;828 else829 return false;830 }831 }832 function MTVATCheckDigit(vatnumber) {833 // Checks the check digits of a Maltese VAT number.834 var total = 0;835 var multipliers = [3, 4, 6, 7, 8, 9];836 // Extract the next digit and multiply by the counter.837 for (var i = 0; i < 6; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];838 // Establish check digits by getting modulus 37.839 total = 37 - total % 37;840 // Compare it with the last character of the VAT number. If it's the same, then it's valid.841 if (total == vatnumber.slice(6, 8) * 1)842 return true;843 else844 return false;845 }846 function NLVATCheckDigit(vatnumber) {847 // Checks the check digits of a Dutch VAT number.848 var total = 0;849 var multipliers = [9, 8, 7, 6, 5, 4, 3, 2];850 // Extract the next digit and multiply by the counter.851 for (var i = 0; i < 8; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];852 // Establish check digits by getting modulus 11.853 total = total % 11;854 if (total > 9) {855 total = 0;856 }857 // Compare it with the last character of the VAT number. If it's the same, then it's valid.858 if (total == vatnumber.slice(8, 9))859 return true;860 else861 return false;862 }863 function NOVATCheckDigit(vatnumber) {864 // Checks the check digits of a Norwegian VAT number.865 // See http://www.brreg.no/english/coordination/number.html866 var total = 0;867 var multipliers = [3, 2, 7, 6, 5, 4, 3, 2];868 // Extract the next digit and multiply by the counter.869 for (var i = 0; i < 8; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];870 // Establish check digits by getting modulus 11. Check digits > 9 are invalid871 total = 11 - total % 11;872 if (total == 11) {873 total = 0;874 }875 if (total < 10) {876 // Compare it with the last character of the VAT number. If it's the same, then it's valid.877 if (total == vatnumber.slice(8, 9))878 return true;879 else880 return false;881 }882 }883 function PLVATCheckDigit(vatnumber) {884 // Checks the check digits of a Polish VAT number.885 var total = 0;886 var multipliers = [6, 5, 7, 2, 3, 4, 5, 6, 7];887 // Extract the next digit and multiply by the counter.888 for (var i = 0; i < 9; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];889 // Establish check digits subtracting modulus 11 from 11.890 total = total % 11;891 if (total > 9) {892 total = 0;893 }894 // Compare it with the last character of the VAT number. If it's the same, then it's valid.895 if (total == vatnumber.slice(9, 10))896 return true;897 else898 return false;899 }900 function PTVATCheckDigit(vatnumber) {901 // Checks the check digits of a Portugese VAT number.902 var total = 0;903 var multipliers = [9, 8, 7, 6, 5, 4, 3, 2];904 // Extract the next digit and multiply by the counter.905 for (var i = 0; i < 8; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];906 // Establish check digits subtracting modulus 11 from 11.907 total = 11 - total % 11;908 if (total > 9) {909 total = 0;910 }911 // Compare it with the last character of the VAT number. If it's the same, then it's valid.912 if (total == vatnumber.slice(8, 9))913 return true;914 else915 return false;916 }917 function ROVATCheckDigit(vatnumber) {918 // Checks the check digits of a Romanian VAT number.919 var multipliers = [7, 5, 3, 2, 1, 7, 5, 3, 2];920 // Extract the next digit and multiply by the counter.921 var VATlen = vatnumber.length;922 multipliers = multipliers.slice(10 - VATlen);923 var total = 0;924 for (var i = 0; i < vatnumber.length - 1; i++) {925 total += Number(vatnumber.charAt(i)) * multipliers[i];926 }927 // Establish check digits by getting modulus 11.928 total = (10 * total) % 11;929 if (total == 10) total = 0;930 // Compare it with the last character of the VAT number. If it's the same, then it's valid.931 if (total == vatnumber.slice(vatnumber.length - 1, vatnumber.length))932 return true;933 else934 return false;935 }936 function RSVATCheckDigit(vatnumber) {937 // Checks the check digits of a Serbian VAT number using ISO 7064, MOD 11-10 for check digit.938 var product = 10;939 var sum = 0;940 var checkdigit = 0;941 for (var i = 0; i < 8; i++) {942 // Extract the next digit and implement the algorithm943 sum = (Number(vatnumber.charAt(i)) + product) % 10;944 if (sum == 0) {945 sum = 10;946 }947 product = (2 * sum) % 11;948 }949 // Now check that we have the right check digit950 if ((product + vatnumber.slice(8, 9) * 1) % 10 == 1)951 return true;952 else953 return false;954 }955 function RUVATCheckDigit(vatnumber) {956 // Checks the check digits of a Russian INN number957 // See http://russianpartner.biz/test_inn.html for algorithm958 var i;959 // 10 digit INN numbers960 if (vatnumber.length == 10) {961 var total = 0;962 var multipliers = [2, 4, 10, 3, 5, 9, 4, 6, 8, 0];963 for (i = 0; i < 10; i++) {964 total += Number(vatnumber.charAt(i)) * multipliers[i];965 }966 total = total % 11;967 if (total > 9) {968 total = total % 10;969 }970 // Compare it with the last character of the VAT number. If it is the same, then it's valid971 if (total == vatnumber.slice(9, 10))972 return true;973 else974 return false;975 // 12 digit INN numbers976 } else if (vatnumber.length == 12) {977 var total1 = 0;978 var multipliers1 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0];979 var total2 = 0;980 var multipliers2 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0];981 for (i = 0; i < 11; i++) total1 += Number(vatnumber.charAt(i)) * multipliers1[i];982 total1 = total1 % 11;983 if (total1 > 9) {984 total1 = total1 % 10;985 }986 for (i = 0; i < 11; i++) total2 += Number(vatnumber.charAt(i)) * multipliers2[i];987 total2 = total2 % 11;988 if (total2 > 9) {989 total2 = total2 % 10;990 }991 // Compare the first check with the 11th character and the second check with the 12th and last992 // character of the VAT number. If they're both the same, then it's valid993 if ((total1 == vatnumber.slice(10, 11)) && (total2 == vatnumber.slice(11, 12)))994 return true;995 else996 return false;997 }998 }999 function SEVATCheckDigit(vatnumber) {1000 var i;1001 // Calculate R where R = R1 + R3 + R5 + R7 + R9, and Ri = INT(Ci/5) + (Ci*2) modulo 101002 var R = 0;1003 var digit;1004 for (i = 0; i < 9; i = i + 2) {1005 digit = Number(vatnumber.charAt(i));1006 R += Math.floor(digit / 5) + ((digit * 2) % 10);1007 }1008 // Calculate S where S = C2 + C4 + C6 + C81009 var S = 0;1010 for (i = 1; i < 9; i = i + 2) S += Number(vatnumber.charAt(i));1011 // Calculate the Check Digit1012 var cd = (10 - (R + S) % 10) % 10;1013 // Compare it with the last character of the VAT number. If it's the same, then it's valid.1014 if (cd == vatnumber.slice(9, 10))1015 return true;1016 else1017 return false;1018 }1019 function SIVATCheckDigit(vatnumber) {1020 // Checks the check digits of a Slovenian VAT number.1021 var total = 0;1022 var multipliers = [8, 7, 6, 5, 4, 3, 2];1023 // Extract the next digit and multiply by the counter.1024 for (var i = 0; i < 7; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];1025 // Establish check digits using modulus 111026 total = 11 - total % 11;1027 if (total == 10) {1028 total = 0;1029 }1030 // Compare the number with the last character of the VAT number. If it is the1031 // same, then it's a valid check digit.1032 if (total != 11 && total == vatnumber.slice(7, 8))1033 return true;1034 else1035 return false;1036 }1037 function SKVATCheckDigit(vatnumber) {1038 // Checks the check digits of a Slovakian VAT number.1039 // Check that the modulus of the whole VAT number is 0 - else error1040 if (Number(vatnumber % 11) == 0)1041 return true;1042 else1043 return false;1044 }1045 return checkVATNumber;...

Full Screen

Full Screen

euvat_check.js

Source:euvat_check.js Github

copy

Full Screen

1function isValidVATID (toCheck) {2 3 // Array holds the regular expressions for the valid VAT number4 var vatexp = new Array ();5 6 // To change the default country (e.g. from the UK to Germany - DE):7 // 1. Change the country code in the defCCode variable below to "DE".8 // 2. Remove the question mark from the regular expressions associated 9 // with the UK VAT number: i.e. "(GB)?" -> "(GB)"10 // 3. Add a question mark into the regular expression associated with11 // Germany's number following the country code: i.e. "(DE)" -> "(DE)?"12 13 var defCCode = "GB";14 vatexp.push (/^(AT)U(\d{8})$/); //** Austria15 vatexp.push (/^(BE)(\d{9,10}\d?)$/); //** Belgium 16 vatexp.push (/^(CY)\d{8}[A-Z]$/); // Cyprus 17 vatexp.push (/^(CZ)(\d{8,10})(\d{3})?$/); //** Czech Republic18 vatexp.push (/^(DK)((\d{8}))$/); //** Denmark 19 vatexp.push (/^(EE)(\d{9})$/); //** Estonia 20 vatexp.push (/^(FI)(\d{8})$/); //** Finland 21 vatexp.push (/^(FR)(\d{11})$/); //** France (1)22 vatexp.push (/^(FR)[(A-H)|(J-N)|(P-Z)]\d{10}$/); // France (2)23 vatexp.push (/^(FR)\d[(A-H)|(J-N)|(P-Z)]\d{9}$/); // France (3)24 vatexp.push (/^(FR)[(A-H)|(J-N)|(P-Z)]{2}\d{9}$/); // France (4)25 vatexp.push (/^(DE)(\d{9})$/); //** Germany 26 vatexp.push (/^(EL)(\d{8,9})$/); //** Greece 27 vatexp.push (/^(HU)(\d{8})$/); //** Hungary 28 vatexp.push (/^(IE)(\d{7}[A-W])$/); //** Ireland (1)29 vatexp.push (/^(IE)([7-9][A-Z]\d{5}[A-W])$/); //** Ireland (2)30 vatexp.push (/^(IT)(\d{11})$/); //** Italy 31 vatexp.push (/^(LV)(\d{11})$/); //** Latvia 32 vatexp.push (/^(LT)(\d{9}|\d{12})$/); //** Lithunia33 vatexp.push (/^(LU)(\d{8})$/); //** Luxembourg 34 vatexp.push (/^(MT)(\d{8})$/); //** Malta35 vatexp.push (/^(NL)(\d{9})B\d{2}$/); //** Netherlands36 vatexp.push (/^(PL)(\d{10})$/); //** Poland37 vatexp.push (/^(PT)(\d{9})$/); //** Portugal38 vatexp.push (/^(RO)(\d{10})$/); // Romania39 vatexp.push (/^(SL)(\d{8})$/); //** Slovenia40 vatexp.push (/^(SK)(\d{9}|\d{10})$/); // Slovakia Republic41 vatexp.push (/^(ES)([A-Z]\d{8})$/); //** Spain (1)42 vatexp.push (/^(ES)(\d{8}[A-Z])$/); // Spain (2)43 vatexp.push (/^(ES)([A-Z]\d{7}[A-Z])$/); //** Spain (3)44 vatexp.push (/^(SE)(\d{10}\d[1-4])$/); //** Sweden45 vatexp.push (/^(GB)?(\d{9})$/); //** UK (1)46 vatexp.push (/^(GB)?(\d{9})\d{3}$/); //** UK (2)47 vatexp.push (/^(GB)?GD\d{3}$/); //** UK (3)48 vatexp.push (/^(GB)?HA\d{3}$/); //** UK (4)49 // Load up the string to check50 var VATNumber = toCheck.toUpperCase();51 52 // Remove spaces from the VAT number to help validation53 var chars = [" ","-",",","."];54 for ( var i=0; i<chars.length; i++) {55 while (VATNumber.indexOf(chars[i])!= -1) {56 VATNumber = VATNumber.slice (0,VATNumber.indexOf(chars[i])) + VATNumber.slice (VATNumber.indexOf(chars[i])+1);57 }58 }59 // Assume we're not going to find a valid VAT number60 var valid = false;61 62 // Check the string against the types of VAT numbers63 for (i=0; i<vatexp.length; i++) {64 if (vatexp[i].test(VATNumber)) {65 66 var cCode = RegExp.$1; // Isolate country code67 var cNumber = RegExp.$2; // Isolate the number68 if (cCode.length == 0) cCode = defCCode; // Set up default country code69 70 // Now look at the check digits for those countries we know about.71 switch (cCode) { 72 case "AT":73 valid = ATVATCheckDigit (cNumber);74 break; 75 case "BE":76 valid = BEVATCheckDigit (cNumber);77 break; 78 case "CZ":79 valid = CZVATCheckDigit (cNumber);80 break; 81 case "DK":82 valid = DKVATCheckDigit (cNumber);83 break; 84 case "DE":85 valid = DEVATCheckDigit (cNumber);86 break; 87 case "EE":88 valid = EEVATCheckDigit (cNumber);89 break; 90 case "EL":91 valid = ELVATCheckDigit (cNumber);92 break; 93 case "ES":94 valid = ESVATCheckDigit (cNumber);95 break; 96 case "FI":97 valid = FIVATCheckDigit (cNumber);98 break; 99 case "FR":100 valid = FRVATCheckDigit (cNumber);101 break; 102 case "GB":103 valid = UKVATCheckDigit (cNumber);104 break; 105 case "HU":106 valid = HUVATCheckDigit (cNumber);107 break; 108 case "IE":109 valid = IEVATCheckDigit (cNumber);110 break; 111 case "IT":112 valid = ITVATCheckDigit (cNumber);113 break; 114 case "LT":115 valid = LTVATCheckDigit (cNumber);116 break; 117 case "LU":118 valid = LUVATCheckDigit (cNumber);119 break; 120 case "LV":121 valid = LVVATCheckDigit (cNumber);122 break; 123 case "MT":124 valid = MTVATCheckDigit (cNumber);125 break; 126 case "NL":127 valid = NLVATCheckDigit (cNumber);128 break; 129 case "PL": 130 valid = PLVATCheckDigit (cNumber);131 break; 132 case "PT":133 valid = PTVATCheckDigit (cNumber);134 break; 135 case "SE":136 valid = SEVATCheckDigit (cNumber);137 break; 138 case "SI":139 valid = SLVATCheckDigit (cNumber);140 break; 141 default:142 valid = true;143 }144 145 // Load new VAT number back into the form element146 if (valid) valid = VATNumber;147 148 // We have found that the number is valid - break from loop149 break;150 }151 }152 153 // Return with either an error or the reformatted VAT number154 return valid;155}156function ATVATCheckDigit (vatnumber) {157 // Checks the check digits of an Austrian VAT number.158 159 var total = 0;160 var multipliers = [1,2,1,2,1,2,1];161 var temp = 0;162 163 // Extract the next digit and multiply by the appropriate multiplier. 164 for (var i = 0; i < 7; i++) {165 temp = Number(vatnumber.charAt(i)) * multipliers[i];166 if (temp > 9)167 total = total + Math.floor(temp/10) + temp%10168 else169 total = total + temp;170 } 171 172 // Establish check digit.173 total = 10 - (total+4) % 10; 174 if (total == 10) total = 0;175 176 // Compare it with the last character of the VAT number. If it is the same, 177 // then it's a valid check digit.178 if (total == vatnumber.slice (7,8)) 179 return true180 else 181 return false;182}183function BEVATCheckDigit (vatnumber) {184 // Checks the check digits of a Belgium VAT number.185 186 // Nine digit numbers have a 0 inserted at the front.187 if (vatnumber.length == 9) vatnumber = "0" + vatnumber;188 189 if (97 - vatnumber.slice (0,8) % 97 == vatnumber.slice (8,10)) 190 return true191 else 192 return false;193}194function CZVATCheckDigit (vatnumber) {195 // Checks the check digits of a Czech Republic VAT number.196 197 var total = 0;198 var multipliers = [8,7,6,5,4,3,2];199 200 // Only do check digit validation for standard VAT numbers201 if (vatnumber.length != 8) return true;202 203 // Extract the next digit and multiply by the counter.204 for (var i = 0; i < 7; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];205 206 // Establish check digit.207 total = 11 - total % 11;208 if (total == 10) total = 0; 209 if (total == 11) total = 1; 210 211 // Compare it with the last character of the VAT number. If it is the same, 212 // then it's a valid check digit.213 if (total == vatnumber.slice (7,8)) 214 return true215 else 216 return false;217}218function DEVATCheckDigit (vatnumber) {219 // Checks the check digits of a German VAT number.220 221 var product = 10;222 var sum = 0; 223 var checkdigit = 0; 224 for (var i = 0; i < 8; i++) {225 226 // Extract the next digit and implement perculiar algorithm!.227 sum = (Number(vatnumber.charAt(i)) + product) % 10;228 if (sum == 0) {sum = 10};229 product = (2 * sum) % 11;230 }231 232 // Establish check digit. 233 if (11 - product == 10) {checkdigit = 0} else {checkdigit = 11 - product};234 235 // Compare it with the last two characters of the VAT number. If the same, 236 // then it is a valid check digit.237 if (checkdigit == vatnumber.slice (8,9))238 return true239 else 240 return false;241}242function DKVATCheckDigit (vatnumber) {243 // Checks the check digits of a Danish VAT number.244 245 var total = 0;246 var multipliers = [2,7,6,5,4,3,2,1];247 248 // Extract the next digit and multiply by the counter.249 for (var i = 0; i < 8; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];250 251 // Establish check digit.252 total = total % 11;253 254 // The remainder should be 0 for it to be valid..255 if (total == 0) 256 return true257 else 258 return false;259}260function EEVATCheckDigit (vatnumber) {261 // Checks the check digits of an Estonian VAT number.262 263 var total = 0;264 var multipliers = [3,7,1,3,7,1,3,7];265 266 // Extract the next digit and multiply by the counter.267 for (var i = 0; i < 8; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];268 269 // Establish check digits using modulus 10.270 total = 10 - total % 10;271 if (total == 10) total = 0;272 273 // Compare it with the last character of the VAT number. If it is the same, 274 // then it's a valid check digit.275 if (total == vatnumber.slice (8,9))276 return true277 else 278 return false;279}280function ELVATCheckDigit (vatnumber) {281 // Checks the check digits of a Greek VAT number.282 283 var total = 0;284 var multipliers = [256,128,64,32,16,8,4,2];285 286 //eight character numbers should be prefixed with an 0.287 if (vatnumber.length == 8) {vatnumber = "0" + vatnumber};288 289 // Extract the next digit and multiply by the counter.290 for (var i = 0; i < 8; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];291 292 // Establish check digit.293 total = total % 11;294 if (total > 9) {total = 0;}; 295 296 // Compare it with the last character of the VAT number. If it is the same, 297 // then it's a valid check digit.298 if (total == vatnumber.slice (8,9)) 299 return true300 else 301 return false;302}303function ESVATCheckDigit (vatnumber) {304 // Checks the check digits of a Spanish VAT number.305 306 var total = 0; 307 var temp = 0;308 var multipliers = [2,1,2,1,2,1,2];309 var esexp = new Array ();310 var i = 0;311 esexp.push (/^[A-H]\d{8}$/);312 esexp.push (/^[N|P|Q|S]\d{7}[A-Z]$/);313 314 // With profit companies315 if (esexp[0].test(vatnumber)) {316 317 // Extract the next digit and multiply by the counter.318 for (i = 0; i < 7; i++) {319 temp = Number(vatnumber.charAt(i+1)) * multipliers[i];320 if (temp > 9) 321 total = total + Math.floor(temp/10) + temp%10 322 else 323 total = total + temp;324 } 325 326 // Now calculate the check digit itself. 327 total = 10 - total % 10;328 if (total == 10) {total = 0;}329 330 // Compare it with the last character of the VAT number. If it is the same, 331 // then it's a valid check digit.332 if (total == vatnumber.slice (8,9)) 333 return true334 else 335 return false;336 }337 338 // Non-profit companies339 else if (esexp[1].test(vatnumber)) {340 341 // Extract the next digit and multiply by the counter.342 for (i = 0; i < 7; i++) {343 temp = Number(vatnumber.charAt(i+1)) * multipliers[i];344 if (temp > 9) 345 total = total + Math.floor(temp/10) + temp%10 346 else 347 total = total + temp;348 } 349 350 // Now calculate the check digit itself.351 total = 10 - total % 10;352 total = String.fromCharCode(total+64);353 354 // Compare it with the last character of the VAT number. If it is the same, 355 // then it's a valid check digit.356 if (total == vatnumber.slice (8,9)) 357 return true358 else 359 return false;360 }361 else return true;362}363function FIVATCheckDigit (vatnumber) {364 // Checks the check digits of a Finnish VAT number.365 366 var total = 0; 367 var multipliers = [7,9,10,5,8,4,2];368 369 // Extract the next digit and multiply by the counter.370 for (var i = 0; i < 7; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];371 372 // Establish check digit.373 total = 11 - total % 11;374 if (total > 9) {total = 0;}; 375 376 // Compare it with the last character of the VAT number. If it is the same, 377 // then it's a valid check digit.378 if (total == vatnumber.slice (7,8)) 379 return true380 else 381 return false;382}383function FRVATCheckDigit (vatnumber) {384 // Checks the check digits of a French VAT number.385 386 if (!(/^\d{11}$/).test(vatnumber)) return true;387 388 // Extract the last nine digits as an integer.389 var total = vatnumber.substring(2); 390 391 // Establish check digit.392 total = (total*100+12) % 97;393 394 // Compare it with the last character of the VAT number. If it is the same, 395 // then it's a valid check digit.396 if (total == vatnumber.slice (0,2)) 397 return true398 else 399 return false;400}401function HUVATCheckDigit (vatnumber) {402 // Checks the check digits of a Hungarian VAT number.403 404 var total = 0;405 var multipliers = [9,7,3,1,9,7,3];406 407 // Extract the next digit and multiply by the counter.408 for (var i = 0; i < 7; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];409 410 // Establish check digit.411 total = 10 - total % 10; 412 if (total == 10) total = 0;413 414 // Compare it with the last character of the VAT number. If it is the same, 415 // then it's a valid check digit.416 if (total == vatnumber.slice (7,8)) 417 return true418 else 419 return false;420}421function IEVATCheckDigit (vatnumber) {422 // Checks the check digits of an Irish VAT number.423 424 var total = 0; 425 var multipliers = [8,7,6,5,4,3,2];426 427 // If the code is in the old format, we need to convert it to the new.428 if (/^\d[A-Z]/.test(vatnumber)) {429 vatnumber = "0" + vatnumber.substring(2,7) + vatnumber.substring(0,1) + vatnumber.substring(7,8);430 }431 432 // Extract the next digit and multiply by the counter.433 for (var i = 0; i < 7; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];434 435 // Establish check digit using modulus 23, and translate to char. equivalent.436 total = total % 23;437 if (total == 0)438 total = "W"439 else440 total = String.fromCharCode(total+64);441 442 // Compare it with the last character of the VAT number. If it is the same, 443 // then it's a valid check digit.444 if (total == vatnumber.slice (7,8)) 445 return true446 else 447 return false;448}449function ITVATCheckDigit (vatnumber) {450 // Checks the check digits of an Italian VAT number.451 452 var total = 0;453 var multipliers = [1,2,1,2,1,2,1,2,1,2];454 var temp;455 456 // The last three digits are the issuing office, and cannot exceed more 201457 temp=Number(vatnumber.slice(0,7));458 if (temp==0) return false;459 temp=Number(vatnumber.slice(7,10));460 if ((temp<1) || (temp>201)) return false;461 462 // Extract the next digit and multiply by the appropriate 463 for (var i = 0; i < 10; i++) {464 temp = Number(vatnumber.charAt(i)) * multipliers[i];465 if (temp > 9) 466 total = total + Math.floor(temp/10) + temp%10 467 else 468 total = total + temp;469 }470 471 // Establish check digit.472 total = 10 - total % 10;473 if (total > 9) {total = 0;}; 474 475 // Compare it with the last character of the VAT number. If it is the same, 476 // then it's a valid check digit.477 if (total == vatnumber.slice (10,11)) 478 return true479 else 480 return false;481}482function LTVATCheckDigit (vatnumber) {483 // Checks the check digits of a Lithuanian VAT number.484 485 // Only do check digit validation for standard VAT numbers486 if (vatnumber.length != 9) return true;487 488 // Extract the next digit and multiply by the counter+1.489 var total = 0;490 for (var i = 0; i < 8; i++) total = total + Number(vatnumber.charAt(i)) * (i+1);491 492 // Can have a double check digit calculation!493 if (total % 11 == 10) {494 var multipliers = [3,4,5,6,7,8,9,1];495 total = 0;496 for (i = 0; i < 8; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];497 }498 499 // Establish check digit.500 total = total % 11;501 if (total == 10) {total = 0;}; 502 503 // Compare it with the last character of the VAT number. If it is the same, 504 // then it's a valid check digit.505 if (total == vatnumber.slice (8,9)) 506 return true507 else 508 return false;509}510function LUVATCheckDigit (vatnumber) {511 // Checks the check digits of a Luxembourg VAT number.512 513 if (vatnumber.slice (0,6) % 89 == vatnumber.slice (6,8)) 514 return true515 else 516 return false;517}518function LVVATCheckDigit (vatnumber) {519 // Checks the check digits of a Latvian VAT number.520 521 // Only check the legal bodies522 if ((/^[0-3]/).test(vatnumber)) return true; 523 524 var total = 0;525 var multipliers = [9,1,4,8,3,10,2,5,7,6];526 527 // Extract the next digit and multiply by the counter.528 for (var i = 0; i < 10; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];529 530 // Establish check digits by getting modulus 11.531 if (total%11 == 4 && vatnumber[0] ==9) total = total - 45;532 if (total%11 == 4) 533 total = 4 - total%11534 else if (total%11 > 4) 535 total = 14 - total%11536 else if (total%11 < 4) 537 total = 3 - total%11;538 539 // Compare it with the last character of the VAT number. If it is the same, 540 // then it's a valid check digit.541 if (total == vatnumber.slice (10,11)) 542 return true543 else 544 return false;545}546function MTVATCheckDigit (vatnumber) {547 // Checks the check digits of a Maltese VAT number.548 549 var total = 0;550 var multipliers = [3,4,6,7,8,9];551 552 // Extract the next digit and multiply by the counter.553 for (var i = 0; i < 6; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];554 555 // Establish check digits by getting modulus 37.556 total = 37 - total % 37;557 558 // Compare it with the last character of the VAT number. If it is the same, 559 // then it's a valid check digit.560 if (total == vatnumber.slice (6,8) * 1) 561 return true562 else 563 return false;564}565function NLVATCheckDigit (vatnumber) {566 // Checks the check digits of a Dutch VAT number.567 568 var total = 0; // 569 var multipliers = [9,8,7,6,5,4,3,2];570 571 // Extract the next digit and multiply by the counter.572 for (var i = 0; i < 8; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];573 574 // Establish check digits by getting modulus 11.575 total = total % 11;576 if (total > 9) {total = 0;}; 577 578 // Compare it with the last character of the VAT number. If it is the same, 579 // then it's a valid check digit.580 if (total == vatnumber.slice (8,9)) 581 return true582 else 583 return false;584}585function PLVATCheckDigit (vatnumber) {586 // Checks the check digits of a Polish VAT number.587 588 var total = 0;589 var multipliers = [6,5,7,2,3,4,5,6,7];590 591 // Extract the next digit and multiply by the counter.592 for (var i = 0; i < 9; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];593 594 // Establish check digits subtracting modulus 11 from 11.595 total = total % 11;596 if (total > 9) {total = 0;};597 598 // Compare it with the last character of the VAT number. If it is the same, 599 // then it's a valid check digit.600 if (total == vatnumber.slice (9,10)) 601 return true602 else 603 return false;604}605function PTVATCheckDigit (vatnumber) {606 // Checks the check digits of a Portugese VAT number.607 608 var total = 0;609 var multipliers = [9,8,7,6,5,4,3,2];610 611 // Extract the next digit and multiply by the counter.612 for (var i = 0; i < 8; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];613 614 // Establish check digits subtracting modulus 11 from 11.615 total = 11 - total % 11;616 if (total > 9) {total = 0;};617 618 // Compare it with the last character of the VAT number. If it is the same, 619 // then it's a valid check digit.620 if (total == vatnumber.slice (8,9)) 621 return true622 else 623 return false;624}625function SEVATCheckDigit (vatnumber) {626 // Checks the check digits of a Swedish VAT number.627 628 var total = 0;629 var multipliers = [2,1,2,1,2,1,2,1,2];630 var temp = 0;631 632 // Extract the next digit and multiply by the appropriate multiplier.633 for (var i = 0; i < 9; i++) {634 temp = Number(vatnumber.charAt(i)) * multipliers[i];635 if (temp > 9)636 total = total + Math.floor(temp/10) + temp%10637 else 638 total = total + temp;639 }640 641 // Establish check digits by subtracting mod 10 of total from 10.642 total = 10 - (total % 10); 643 if (total == 10) total = 0;644 645 // Compare it with the 10th character of the VAT number. If it is the same, 646 // then it's a valid check digit.647 if (total == vatnumber.slice (9,10)) 648 return true649 else 650 return false;651}652function SKVATCheckDigit (vatnumber) {653 // Checks the check digits of a Slovak VAT number.654 655 var total = 0; 656 var multipliers = [8,7,6,5,4,3,2];657 658 // Extract the next digit and multiply by the counter.659 for (var i = 3; i < 9; i++) {660 total = total + Number(vatnumber.charAt(i)) * multipliers[i-3];661 } 662 663 // Establish check digits by getting modulus 11.664 total = 11 - total % 11;665 if (total > 9) total = total - 10; 666 667 // Compare it with the last character of the VAT number. If it is the same, 668 // then it's a valid check digit.669 if (total == vatnumber.slice (9,10)) 670 return true671 else 672 return false;673}674function SLVATCheckDigit (vatnumber) {675 // Checks the check digits of a Slovenian VAT number.676 677 var total = 0; 678 var multipliers = [8,7,6,5,4,3,2];679 680 // Extract the next digit and multiply by the counter.681 for (var i = 0; i < 7; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];682 683 // Establish check digits by subtracting 97 from total until negative.684 total = 11 - total % 11;685 if (total > 9) {total = 0;}; 686 687 // Compare the number with the last character of the VAT number. If it is the 688 // same, then it's a valid check digit.689 if (total == vatnumber.slice (7,8)) 690 return true691 else 692 return false;693}694function UKVATCheckDigit (vatnumber) {695 // Checks the check digits of a UK VAT number.696 697 // Only inspect check digit of 9 character numbers698 if (vatnumber.length != 9) return true;699 700 var multipliers = [8,7,6,5,4,3,2];701 var total = 0;702 703 // Extract the next digit and multiply by the counter.704 for (var i = 0; i < 7; i++) total = total + Number(vatnumber.charAt(i)) * multipliers[i];705 706 // Establish check digits by subtracting 97 from total until negative.707 while (total > 0) {total = total - 97;} 708 709 // Get the absolute value and compare it with the last two characters of the710 // VAT number. If the same, then it is a valid check digit.711 total = Math.abs(total);712 if (total == vatnumber.slice (7,9)) 713 return true 714 else 715 return false;...

Full Screen

Full Screen

jquery.flipper-responsive.js

Source:jquery.flipper-responsive.js Github

copy

Full Screen

1jQuery(function($){2 // https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/repeat3 if (!String.prototype.repeat) {4 String.prototype.repeat = function(count) {5 'use strict';6 if (this == null) {7 throw new TypeError('can\'t convert ' + this + ' to object');8 }9 var str = '' + this;10 count = +count;11 if (count != count) {12 count = 0;13 }14 if (count < 0) {15 throw new RangeError('repeat count must be non-negative');16 }17 if (count == Infinity) {18 throw new RangeError('repeat count must be less than infinity');19 }20 count = Math.floor(count);21 if (str.length == 0 || count == 0) {22 return '';23 }24 // Обеспечение того, что count является 31-битным целым числом, позволяет нам значительно25 // соптимизировать главную часть функции. Впрочем, большинство современных (на август26 // 2014 года) браузеров не обрабатывают строки, длиннее 1 << 28 символов, так что:27 if (str.length * count >= 1 << 28) {28 throw new RangeError('repeat count must not overflow maximum string size');29 }30 var rpt = '';31 for (var i = 0; i < count; i++) {32 rpt += str;33 }34 return rpt;35 }36 }37 // https://github.com/uxitten/polyfill/blob/master/string.polyfill.js38 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart39 if (!String.prototype.padStart) {40 String.prototype.padStart = function padStart(targetLength,padString) {41 targetLength = targetLength>>0; //truncate if number or convert non-number to 0;42 padString = String((typeof padString !== 'undefined' ? padString : ' '));43 if (this.length > targetLength) {44 return String(this);45 }46 else {47 targetLength = targetLength-this.length;48 if (targetLength > padString.length) {49 padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed50 }51 return padString.slice(0,targetLength) + String(this);52 }53 };54 }55 $.fn.flipper = function(action, options) {56 var $flipper = $(this);57 var action = action || 'init';58 var settings = $.extend({59 // defaults.60 reverse: $flipper.data('reverse') || false,61 datetime: $flipper.data('datetime') || 'now',62 template: $flipper.data('template') || 'HH:ii:ss',63 labels: $flipper.data('labels') || 'Hours|Minutes|Seconds',64 preload: true65 }, options );66 console.log(settings.reverse);67 if(action === 'init'){68 if($flipper.hasClass('flipper-initialized')){69 console.warn('Flipper already initialized.');70 return;71 }72 $flipper.addClass( "flipper-initialized" );73 var templateParts = settings.template.split('|');74 var labelsArray = settings.labels.split('|');75 var n;76 templateParts.forEach(function(part, index){77 if(index > 0){78 $flipper.append('<div class="flipper-group flipper-delimiter">:</div>');79 }80 $flipper.append('<div class="flipper-group flipper-' + part + '"></div>');81 var $part = $flipper.find('.flipper-group.flipper-' + part);82 if(typeof labelsArray[index] !== 'undefined'){83 $part.append('<label>' + labelsArray[index] + '</label>');84 }85 if(part === 'd' || part === 'H' || part === 'i' || part === 's'){86 var rev = settings.reverse ? 'reverse' : '';87 $part.append('<div class="flipper-digit ' + rev + '"></div>');88 }89 if(part === 'dd' || part === 'ddd' || part === 'HH' || part === 'ii' || part === 'ss'){90 var rev = settings.reverse ? 'reverse' : '';91 $part.append('<div class="flipper-digit ' + rev + '"></div>');92 $part.append('<div class="flipper-delimiter"></div>');93 $part.append('<div class="flipper-digit ' + rev + '"></div>');94 if(part === 'ddd'){95 $part.append('<div class="flipper-delimiter"></div>');96 $part.append('<div class="flipper-digit ' + rev + '"></div>');97 }98 }99 if(part === 'd'){100 for(n = 0; n <= 31; n++){101 $part.find('.flipper-digit:eq(0)').append('<div class="digit-face">' + n + '</div>');102 $part.find('.flipper-digit:eq(1)').append('<div class="digit-face">' + n + '</div>');103 }104 }105 if(part === 'H'){106 for(n = 0; n <= 23; n++){107 $part.find('.flipper-digit:eq(0)').append('<div class="digit-face">' + n + '</div>');108 $part.find('.flipper-digit:eq(1)').append('<div class="digit-face">' + n + '</div>');109 }110 }111 if(part === 'i' || part === 's'){112 for(n = 0; n <= 59; n++){113 $part.find('.flipper-digit:eq(0)').append('<div class="digit-face">' + n + '</div>');114 $part.find('.flipper-digit:eq(1)').append('<div class="digit-face">' + n + '</div>');115 }116 }117 if(part === 'dd' || part === 'ddd'){118 for(n = 0; n <= 9; n++){119 $part.find('.flipper-digit:eq(0)').append('<div class="digit-face">' + n + '</div>');120 $part.find('.flipper-digit:eq(1)').append('<div class="digit-face">' + n + '</div>');121 if(part === 'ddd'){122 $part.find('.flipper-digit:eq(2)').append('<div class="digit-face">' + n + '</div>');123 }124 }125 }126 if(part === 'HH'){127 for(n = 0; n <= 2; n++){128 $part.find('.flipper-digit:eq(0)').append('<div class="digit-face">' + n + '</div>');129 }130 for(n = 0; n <= 9; n++){131 $part.find('.flipper-digit:eq(1)').append('<div class="digit-face">' + n + '</div>');132 }133 }134 if(part === 'ii' || part === 'ss'){135 for(n = 0; n <= 5; n++){136 $part.find('.flipper-digit:eq(0)').append('<div class="digit-face">' + n + '</div>');137 }138 for(n = 0; n <= 9; n++){139 $part.find('.flipper-digit:eq(1)').append('<div class="digit-face">' + n + '</div>');140 }141 }142 });143 if(settings.preload){144 setFlipperDate($flipper, settings.datetime, false);145 }146 setInterval(function(){147 setFlipperDate($flipper, settings.datetime, true);148 }, 1000);149 upsizeToParent($flipper);150 $(window).on("resize", function(){151 upsizeToParent($flipper);152 });153 }154 var flipTime = 400;155 var $body = $('body');156 function flipDigit($digit){157 if(!$digit.closest('.flipper').is('.flipper-initialized')){158 return;159 }160 if($digit.hasClass('r')){161 setTimeout(function(){162 flipDigit($digit);163 }, flipTime + 1);164 return;165 }166 $digit.addClass('r');167 var $currentTop = $digit.find('.digit-top');168 var $currentTop2 = $digit.find('.digit-top2');169 var $currentBottom = $digit.find('.digit-bottom');170 var $activeDigit = $digit.find('.digit-face.active');171 var $firstDigit = $digit.find('.digit-face:first');172 var $prevDigit = $activeDigit.prev('.digit-face');173 var $nextDigit = $activeDigit.next('.digit-face');174 var $lastDigit = $digit.find('.digit-face:last');175 if($digit.hasClass('reverse')){176 var $next = $prevDigit.length ? $prevDigit : $lastDigit;177 }178 else {179 var $next = $nextDigit.length ? $nextDigit : $firstDigit;180 }181 var current = parseInt($currentTop.html());182 var next = $next.html();183 $digit.find('.digit-next').html(next);184 $digit.find('.digit-face').removeClass('active');185 $next.addClass('active');186 $currentTop.addClass('r');187 $currentTop2.addClass('r');188 $currentBottom.addClass('r');189 if(next.toString() === $digit.attr('data-value')){190 $digit.removeAttr('data-value');191 }192 setTimeout(function(){193 $currentTop.html(next).hide();194 $currentTop2.html(next);195 setTimeout(function(){196 $currentBottom.html(next).removeClass('r');197 $currentTop.removeClass('r').show();198 $currentTop2.html(next).removeClass('r');199 $digit.removeClass('r');200 }, flipTime/2);201 }, flipTime/2);202 }203 function upsizeToParent($flipper) {204 var parentWidth;205 var flipperWidth;206 var maxFontSize = 1000;207 var fontSize = maxFontSize;208 var i = 0;209 var minFontSize = 0;210 $flipper.css('font-size', fontSize + 'px');211 while(i < 20){212 i++;213 parentWidth = $flipper.innerWidth();214 $flipper.css('width', '9999px');215 flipperWidth = 0;216 $flipper.find('.flipper-group').each(function(){217 var w = parseFloat($(this).outerWidth());218 flipperWidth += w;219 });220 if((parentWidth - flipperWidth) < 10 && (parentWidth - flipperWidth) > 0){221 $flipper.css('width', '');222 return;223 }224 if(flipperWidth > parentWidth){225 maxFontSize = fontSize < maxFontSize ? fontSize: maxFontSize;226 }227 else {228 minFontSize = fontSize > minFontSize ? fontSize : minFontSize;229 }230 fontSize = (maxFontSize + minFontSize) / 2;231 $flipper.css('width', '');232 $flipper.css('font-size', fontSize + 'px');233 }234 }235 function setDigitValue(digitIndex, value){236 var $flipper = $('.flipper');237 var $digit = $flipper.find('.flipper-digit:eq(' + digitIndex + ')');238 var currentValue = getDigitValue($digit);239 if(currentValue.toString() === value.toString()){240 return; // has same value, do nothing241 }242 $digit.attr('data-value', value);243 }244 setInterval(function(){245 var $flipper = $('.flipper');246 $flipper.find('.flipper-digit[data-value]').each(function(){247 var $digit = $(this);248 if($digit.find('.active').html() === $digit.attr('data-value')){249 return; //250 }251 if(!$digit.is('.r')){252 flipDigit($digit);253 }254 });255 }, flipTime / 4);256 function formatFlipperDate(dateStr) {257 var a=dateStr.split(" ");258 var d=a[0].split("-");259 var t=a[1].split(":");260 var date = new Date(d[0],(d[1]-1),d[2],t[0],t[1],t[2]);261 return date;262 }263 function addAppearance($flipper){264 $flipper.find('.flipper-digit').each(function(){265 var $digit = $(this);266 var value = $digit.find('.digit-face.active').html();267 $digit.find('.digit-top').remove();268 $digit.find('.digit-top2').remove();269 $digit.find('.digit-bottom').remove();270 $digit.find('.digit-next').remove();271 $digit.prepend('<div class="digit-top">' + value + '</div>');272 $digit.prepend('<div class="digit-top2">' + value + '</div>');273 $digit.prepend('<div class="digit-bottom">' + value + '</div>');274 $digit.prepend('<div class="digit-next"></div>');275 });276 }277 function setFlipperDate($flipper, dateString, animate){278 var animate = animate || false;279 if(!$flipper.is(':visible')){280 $flipper.addClass('flipper-invisible');281 return;282 }283 if($flipper.hasClass('flipper-invisible')){284 $flipper.removeClass('flipper-invisible');285 upsizeToParent($flipper);286 setFlipperDate($flipper, settings.datetime, false);287 }288 var now = Date.now();289 if(dateString === 'now'){290 var now = new Date();291 var seconds = now.getSeconds();292 var minutes = now.getMinutes();293 var hours = now.getHours();294 var days = now.getDate();295 }296 else {297 var timestamp = Date.parse(formatFlipperDate(dateString));298 var remainder = (timestamp - now) / 1000;299 var days = Math.floor(remainder / 60 / 60 / 24);300 remainder -= days * 60 * 60 * 24;301 var hours = Math.floor(remainder / 60 / 60);302 remainder -= hours * 60 * 60;303 var minutes = Math.floor(remainder / 60);304 remainder -= minutes * 60;305 var seconds = Math.floor(remainder);306 }307 var days_str = days.toString().padStart(3, '0');308 var hours_str = hours.toString().padStart(2, '0');309 var minutes_str = minutes.toString().padStart(2, '0');310 var seconds_str = seconds.toString().padStart(2, '0');311 if(animate){312 // one section313 $flipper.find('.flipper-d').find('.flipper-digit:eq(0)').attr('data-value', days);314 $flipper.find('.flipper-H').find('.flipper-digit:eq(0)').attr('data-value', hours);315 $flipper.find('.flipper-i').find('.flipper-digit:eq(0)').attr('data-value', minutes);316 $flipper.find('.flipper-s').find('.flipper-digit:eq(0)').attr('data-value', seconds);317 // two sections318 $flipper.find('.flipper-dd').find('.flipper-digit:eq(0)').attr('data-value', days_str[1]);319 $flipper.find('.flipper-dd').find('.flipper-digit:eq(1)').attr('data-value', days_str[2]);320 $flipper.find('.flipper-HH').find('.flipper-digit:eq(0)').attr('data-value', hours_str[0]);321 $flipper.find('.flipper-HH').find('.flipper-digit:eq(1)').attr('data-value', hours_str[1]);322 $flipper.find('.flipper-ii').find('.flipper-digit:eq(0)').attr('data-value', minutes_str[0]);323 $flipper.find('.flipper-ii').find('.flipper-digit:eq(1)').attr('data-value', minutes_str[1]);324 $flipper.find('.flipper-ss').find('.flipper-digit:eq(0)').attr('data-value', seconds_str[0]);325 $flipper.find('.flipper-ss').find('.flipper-digit:eq(1)').attr('data-value', seconds_str[1]);326 // three sections327 $flipper.find('.flipper-ddd').find('.flipper-digit:eq(0)').attr('data-value', days_str[0]);328 $flipper.find('.flipper-ddd').find('.flipper-digit:eq(1)').attr('data-value', days_str[1]);329 $flipper.find('.flipper-ddd').find('.flipper-digit:eq(2)').attr('data-value', days_str[2]);330 }331 else {332 $flipper.find('.flipper-group .flipper-digit').removeAttr('data-value');333 $flipper.find('.digit-face.active').removeClass('active');334 // one section335 $flipper.find('.flipper-d .flipper-digit:eq(0) .digit-face:contains(' + days + ')').addClass('active');336 $flipper.find('.flipper-H .flipper-digit:eq(0) .digit-face:contains(' + hours + ')').addClass('active');337 $flipper.find('.flipper-i .flipper-digit:eq(0) .digit-face:contains(' + minutes + ')').addClass('active');338 $flipper.find('.flipper-s .flipper-digit:eq(0) .digit-face:contains(' + seconds + ')').addClass('active');339 // two sections340 $flipper.find('.flipper-dd .flipper-digit:eq(0) .digit-face:contains(' + days_str[1] + ')').addClass('active');341 $flipper.find('.flipper-dd .flipper-digit:eq(1) .digit-face:contains(' + days_str[2] + ')').addClass('active');342 $flipper.find('.flipper-HH .flipper-digit:eq(0) .digit-face:contains(' + hours_str[0] + ')').addClass('active');343 $flipper.find('.flipper-HH .flipper-digit:eq(1) .digit-face:contains(' + hours_str[1] + ')').addClass('active');344 $flipper.find('.flipper-ii .flipper-digit:eq(0) .digit-face:contains(' + minutes_str[0] + ')').addClass('active');345 $flipper.find('.flipper-ii .flipper-digit:eq(1) .digit-face:contains(' + minutes_str[1] + ')').addClass('active');346 $flipper.find('.flipper-ss .flipper-digit:eq(0) .digit-face:contains(' + seconds_str[0] + ')').addClass('active');347 $flipper.find('.flipper-ss .flipper-digit:eq(1) .digit-face:contains(' + seconds_str[1] + ')').addClass('active');348 // three sections349 $flipper.find('.flipper-ddd .flipper-digit:eq(0) .digit-face:contains(' + days_str[0] + ')').addClass('active');350 $flipper.find('.flipper-ddd .flipper-digit:eq(1) .digit-face:contains(' + days_str[1] + ')').addClass('active');351 $flipper.find('.flipper-ddd .flipper-digit:eq(2) .digit-face:contains(' + days_str[2] + ')').addClass('active');352 addAppearance($flipper);353 }354 }355 };...

Full Screen

Full Screen

basen.js

Source:basen.js Github

copy

Full Screen

1// Copyright 2007 The Closure Library Authors. All Rights Reserved.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS-IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14/**15 * @fileoverview Numeric base conversion library. Works for arbitrary bases and16 * arbitrary length numbers.17 *18 * For base-64 conversion use base64.js because it is optimized for the specific19 * conversion to base-64 while this module is generic. Base-64 is defined here20 * mostly for demonstration purpose.21 *22 * TODO: Make base64 and baseN classes that have common interface. (Perhaps...)23 *24 */25goog.provide('goog.crypt.baseN');26/**27 * Base-2, i.e. '01'.28 * @type {string}29 */30goog.crypt.baseN.BASE_BINARY = '01';31/**32 * Base-8, i.e. '01234567'.33 * @type {string}34 */35goog.crypt.baseN.BASE_OCTAL = '01234567';36/**37 * Base-10, i.e. '0123456789'.38 * @type {string}39 */40goog.crypt.baseN.BASE_DECIMAL = '0123456789';41/**42 * Base-16 using lower case, i.e. '0123456789abcdef'.43 * @type {string}44 */45goog.crypt.baseN.BASE_LOWERCASE_HEXADECIMAL = '0123456789abcdef';46/**47 * Base-16 using upper case, i.e. '0123456789ABCDEF'.48 * @type {string}49 */50goog.crypt.baseN.BASE_UPPERCASE_HEXADECIMAL = '0123456789ABCDEF';51/**52 * The more-known version of the BASE-64 encoding. Uses + and / characters.53 * @type {string}54 */55goog.crypt.baseN.BASE_64 =56 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';57/**58 * URL-safe version of the BASE-64 encoding.59 * @type {string}60 */61goog.crypt.baseN.BASE_64_URL_SAFE =62 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';63/**64 * Converts a number from one numeric base to another.65 *66 * The bases are represented as strings, which list allowed digits. Each digit67 * should be unique. The bases can either be user defined, or any of68 * goog.crypt.baseN.BASE_xxx.69 *70 * The number is in human-readable format, most significant digit first, and is71 * a non-negative integer. Base designators such as $, 0x, d, b or h (at end)72 * will be interpreted as digits, so avoid them. Leading zeros will be trimmed.73 *74 * Note: for huge bases the result may be inaccurate because of overflowing75 * 64-bit doubles used by JavaScript for integer calculus. This may happen76 * if the product of the number of digits in the input and output bases comes77 * close to 10^16, which is VERY unlikely (100M digits in each base), but78 * may be possible in the future unicode world. (Unicode 3.2 has less than 100K79 * characters. However, it reserves some more, close to 1M.)80 *81 * @param {string} number The number to convert.82 * @param {string} inputBase The numeric base the number is in (all digits).83 * @param {string} outputBase Requested numeric base.84 * @return {string} The converted number.85 */86goog.crypt.baseN.recodeString = function(number, inputBase, outputBase) {87 if (outputBase == '') {88 throw Error('Empty output base');89 }90 // Check if number is 0 (special case when we don't want to return '').91 var isZero = true;92 for (var i = 0, n = number.length; i < n; i++) {93 if (number.charAt(i) != inputBase.charAt(0)) {94 isZero = false;95 break;96 }97 }98 if (isZero) {99 return outputBase.charAt(0);100 }101 var numberDigits = goog.crypt.baseN.stringToArray_(number, inputBase);102 var inputBaseSize = inputBase.length;103 var outputBaseSize = outputBase.length;104 // result = 0.105 var result = [];106 // For all digits of number, starting with the most significant ...107 for (var i = numberDigits.length - 1; i >= 0; i--) {108 // result *= number.base.109 var carry = 0;110 for (var j = 0, n = result.length; j < n; j++) {111 var digit = result[j];112 // This may overflow for huge bases. See function comment.113 digit = digit * inputBaseSize + carry;114 if (digit >= outputBaseSize) {115 var remainder = digit % outputBaseSize;116 carry = (digit - remainder) / outputBaseSize;117 digit = remainder;118 } else {119 carry = 0;120 }121 result[j] = digit;122 }123 while (carry) {124 var remainder = carry % outputBaseSize;125 result.push(remainder);126 carry = (carry - remainder) / outputBaseSize;127 }128 // result += number[i].129 carry = numberDigits[i];130 var j = 0;131 while (carry) {132 if (j >= result.length) {133 // Extend result with a leading zero which will be overwritten below.134 result.push(0);135 }136 var digit = result[j];137 digit += carry;138 if (digit >= outputBaseSize) {139 var remainder = digit % outputBaseSize;140 carry = (digit - remainder) / outputBaseSize;141 digit = remainder;142 } else {143 carry = 0;144 }145 result[j] = digit;146 j++;147 }148 }149 return goog.crypt.baseN.arrayToString_(result, outputBase);150};151/**152 * Converts a string representation of a number to an array of digit values.153 *154 * More precisely, the digit values are indices into the number base, which155 * is represented as a string, which can either be user defined or one of the156 * BASE_xxx constants.157 *158 * Throws an Error if the number contains a digit not found in the base.159 *160 * @param {string} number The string to convert, most significant digit first.161 * @param {string} base Digits in the base.162 * @return {!Array.<number>} Array of digit values, least significant digit163 * first.164 * @private165 */166goog.crypt.baseN.stringToArray_ = function(number, base) {167 var index = {};168 for (var i = 0, n = base.length; i < n; i++) {169 index[base.charAt(i)] = i;170 }171 var result = [];172 for (var i = number.length - 1; i >= 0; i--) {173 var character = number.charAt(i);174 var digit = index[character];175 if (typeof digit == 'undefined') {176 throw Error('Number ' + number +177 ' contains a character not found in base ' +178 base + ', which is ' + character);179 }180 result.push(digit);181 }182 return result;183};184/**185 * Converts an array representation of a number to a string.186 *187 * More precisely, the elements of the input array are indices into the base,188 * which is represented as a string, which can either be user defined or one of189 * the BASE_xxx constants.190 *191 * Throws an Error if the number contains a digit which is outside the range192 * 0 ... base.length - 1.193 *194 * @param {Array.<number>} number Array of digit values, least significant195 * first.196 * @param {string} base Digits in the base.197 * @return {string} Number as a string, most significant digit first.198 * @private199 */200goog.crypt.baseN.arrayToString_ = function(number, base) {201 var n = number.length;202 var chars = [];203 var baseSize = base.length;204 for (var i = n - 1; i >= 0; i--) {205 var digit = number[i];206 if (digit >= baseSize || digit < 0) {207 throw Error('Number ' + number + ' contains an invalid digit: ' + digit);208 }209 chars.push(base.charAt(digit));210 }211 return chars.join('');...

Full Screen

Full Screen

list.js

Source:list.js Github

copy

Full Screen

1/*jshint smarttabs:true */2/**3 * FlipClock.js4 *5 * @author Justin Kimbrell6 * @copyright 2013 - Objective HTML, LLC7 * @licesnse http://www.opensource.org/licenses/mit-license.php8 */9 10(function($) {11 12 "use strict";13 14 /**15 * The FlipClock List class is used to build the list used to create 16 * the card flip effect. This object fascilates selecting the correct17 * node by passing a specific digit.18 *19 * @param object A FlipClock.Factory object20 * @param mixed This is the digit used to set the clock. If an 21 * object is passed, 0 will be used. 22 * @param object An object of properties to override the default 23 */24 25 FlipClock.List = FlipClock.Base.extend({26 27 /**28 * The digit (0-9)29 */ 30 31 digit: 0,32 33 /**34 * The CSS classes35 */ 36 37 classes: {38 active: 'flip-clock-active',39 before: 'flip-clock-before',40 flip: 'flip' 41 },42 43 /**44 * The parent FlipClock.Factory object45 */ 46 47 factory: false,48 49 /**50 * The jQuery object51 */ 52 53 $el: false,54 /**55 * The jQuery object (deprecated)56 */ 57 58 $obj: false,59 60 /**61 * The items in the list62 */ 63 64 items: [],65 66 /**67 * The last digit68 */ 69 70 lastDigit: 0,71 72 /**73 * Constructor74 *75 * @param object A FlipClock.Factory object76 * @param int An integer use to select the correct digit77 * @param object An object to override the default properties 78 */79 80 constructor: function(factory, digit, options) {81 this.factory = factory;82 this.digit = digit;83 this.lastDigit = digit;84 this.$el = this.createList();85 86 // Depcrated support of the $obj property.87 this.$obj = this.$el;88 if(digit > 0) {89 this.select(digit);90 }91 this.factory.$el.append(this.$el);92 },93 94 /**95 * Select the digit in the list96 *97 * @param int A digit 0-9 98 */99 100 select: function(digit) {101 if(typeof digit === "undefined") {102 digit = this.digit;103 }104 else {105 this.digit = digit;106 }107 if(this.digit != this.lastDigit) {108 var $delete = this.$el.find('.'+this.classes.before).removeClass(this.classes.before);109 this.$el.find('.'+this.classes.active).removeClass(this.classes.active)110 .addClass(this.classes.before);111 this.appendListItem(this.classes.active, this.digit);112 $delete.remove();113 this.lastDigit = this.digit;114 } 115 },116 117 /**118 * Adds the play class to the DOM object119 */120 121 play: function() {122 this.$el.addClass(this.factory.classes.play);123 },124 125 /**126 * Removes the play class to the DOM object 127 */128 129 stop: function() {130 var t = this;131 setTimeout(function() {132 t.$el.removeClass(t.factory.classes.play);133 }, this.factory.timer.interval);134 },135 136 /**137 * Creates the list item HTML and returns as a string 138 */139 140 createListItem: function(css, value) {141 return [142 '<li class="'+(css ? css : '')+'">',143 '<a href="#">',144 '<div class="up">',145 '<div class="shadow"></div>',146 '<div class="inn">'+(value ? value : '')+'</div>',147 '</div>',148 '<div class="down">',149 '<div class="shadow"></div>',150 '<div class="inn">'+(value ? value : '')+'</div>',151 '</div>',152 '</a>',153 '</li>'154 ].join('');155 },156 /**157 * Append the list item to the parent DOM node 158 */159 appendListItem: function(css, value) {160 var html = this.createListItem(css, value);161 this.$el.append(html);162 },163 /**164 * Create the list of digits and appends it to the DOM object 165 */166 167 createList: function() {168 var lastDigit = this.getPrevDigit() ? this.getPrevDigit() : this.digit;169 var html = $([170 '<ul class="'+this.classes.flip+' '+(this.factory.running ? this.factory.classes.play : '')+'">',171 this.createListItem(this.classes.before, lastDigit),172 this.createListItem(this.classes.active, this.digit),173 '</ul>'174 ].join(''));175 176 return html;177 },178 getNextDigit: function() {179 return this.digit == 9 ? 0 : this.digit + 1;180 },181 getPrevDigit: function() {182 return this.digit == 0 ? 9 : this.digit - 1;183 }184 });185 186 ...

Full Screen

Full Screen

jquery.countdown.js

Source:jquery.countdown.js Github

copy

Full Screen

1/*2 * jquery-counter plugin3 *4 * Copyright (c) 2009 Martin Conte Mac Donell <Reflejo@gmail.com>5 * Dual licensed under the MIT and GPL licenses.6 * http://docs.jquery.com/License7 */8jQuery.fn.countdown = function(userOptions)9{10 // Default options11 var options = {12 stepTime: 60,13 // startTime and format MUST follow the same format.14 // also you cannot specify a format unordered (e.g. hh:ss:mm is wrong)15 format: "dd:hh:mm:ss",16 startTime: "01:12:32:55",17 digitImages: 6,18 digitWidth: 53,19 digitHeight: 77,20 timerEnd: function(){},21 image: "digits.png"22 };23 var digits = [], interval;2425 // Draw digits in given container26 var createDigits = function(where) 27 {28 var c = 0;29 // Iterate each startTime digit, if it is not a digit30 // we'll asume that it's a separator31 for (var i = 0; i < options.startTime.length; i++)32 {33 if (parseInt(options.startTime[i]) >= 0) 34 {35 elem = $('<div id="cnt_' + i + '" class="cntDigit" />').css({36 height: options.digitHeight * options.digitImages * 10, 37 float: 'left', background: 'url(\'' + options.image + '\')',38 width: options.digitWidth});39 digits.push(elem);40 margin(c, -((parseInt(options.startTime[i]) * options.digitHeight *41 options.digitImages)));42 digits[c].__max = 9;43 // Add max digits, for example, first digit of minutes (mm) has 44 // a max of 5. Conditional max is used when the left digit has reach45 // the max. For example second "hours" digit has a conditional max of 4 46 switch (options.format[i]) {47 case 'h':48 digits[c].__max = (c % 2 == 0) ? 2: 9;49 if (c % 2 == 0)50 digits[c].__condmax = 4;51 break;52 case 'd': 53 digits[c].__max = 9;54 break;55 case 'm':56 case 's':57 digits[c].__max = (c % 2 == 0) ? 5: 9;58 }59 ++c;60 }61 else 62 elem = $('<div class="cntSeparator"/>').css({float: 'left'})63 .text(options.startTime[i]);6465 where.append(elem)66 }67 };68 69 // Set or get element margin70 var margin = function(elem, val) 71 {72 if (val !== undefined)73 return digits[elem].css({'marginTop': val + 'px'});7475 return parseInt(digits[elem].css('marginTop').replace('px', ''));76 };7778 // Makes the movement. This is done by "digitImages" steps.79 var moveStep = function(elem) 80 {81 digits[elem]._digitInitial = -(digits[elem].__max * options.digitHeight * options.digitImages);82 return function _move() {83 mtop = margin(elem) + options.digitHeight;84 if (mtop == options.digitHeight) {85 margin(elem, digits[elem]._digitInitial);86 if (elem > 0) moveStep(elem - 1)();87 else 88 {89 clearInterval(interval);90 for (var i=0; i < digits.length; i++) margin(i, 0);91 options.timerEnd();92 return;93 }94 if ((elem > 0) && (digits[elem].__condmax !== undefined) && 95 (digits[elem - 1]._digitInitial == margin(elem - 1)))96 margin(elem, -(digits[elem].__condmax * options.digitHeight * options.digitImages));97 return;98 }99100 margin(elem, mtop);101 if (margin(elem) / options.digitHeight % options.digitImages != 0)102 setTimeout(_move, options.stepTime);103104 if (mtop == 0) digits[elem].__ismax = true;105 }106 };107108 $.extend(options, userOptions);109 this.css({height: options.digitHeight, overflow: 'hidden'});110 createDigits(this);111 interval = setInterval(moveStep(digits.length - 1), 1000); ...

Full Screen

Full Screen

isPostalCode.js

Source:isPostalCode.js Github

copy

Full Screen

1import assertString from './util/assertString';2// common patterns3const threeDigit = /^\d{3}$/;4const fourDigit = /^\d{4}$/;5const fiveDigit = /^\d{5}$/;6const sixDigit = /^\d{6}$/;7const patterns = {8 AD: /^AD\d{3}$/,9 AT: fourDigit,10 AU: fourDigit,11 BE: fourDigit,12 BG: fourDigit,13 BR: /^\d{5}-\d{3}$/,14 CA: /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s\-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i,15 CH: fourDigit,16 CZ: /^\d{3}\s?\d{2}$/,17 DE: fiveDigit,18 DK: fourDigit,19 DZ: fiveDigit,20 EE: fiveDigit,21 ES: fiveDigit,22 FI: fiveDigit,23 FR: /^\d{2}\s?\d{3}$/,24 GB: /^(gir\s?0aa|[a-z]{1,2}\d[\da-z]?\s?(\d[a-z]{2})?)$/i,25 GR: /^\d{3}\s?\d{2}$/,26 HR: /^([1-5]\d{4}$)/,27 HU: fourDigit,28 ID: fiveDigit,29 IL: fiveDigit,30 IN: sixDigit,31 IS: threeDigit,32 IT: fiveDigit,33 JP: /^\d{3}\-\d{4}$/,34 KE: fiveDigit,35 LI: /^(948[5-9]|949[0-7])$/,36 LT: /^LT\-\d{5}$/,37 LU: fourDigit,38 LV: /^LV\-\d{4}$/,39 MX: fiveDigit,40 MT: /^[A-Za-z]{3}\s{0,1}\d{4}$/,41 NL: /^\d{4}\s?[a-z]{2}$/i,42 NO: fourDigit,43 NZ: fourDigit,44 PL: /^\d{2}\-\d{3}$/,45 PR: /^00[679]\d{2}([ -]\d{4})?$/,46 PT: /^\d{4}\-\d{3}?$/,47 RO: sixDigit,48 RU: sixDigit,49 SA: fiveDigit,50 SE: /^\d{3}\s?\d{2}$/,51 SI: fourDigit,52 SK: /^\d{3}\s?\d{2}$/,53 TN: fourDigit,54 TW: /^\d{3}(\d{2})?$/,55 UA: fiveDigit,56 US: /^\d{5}(-\d{4})?$/,57 ZA: fourDigit,58 ZM: fiveDigit,59};60export const locales = Object.keys(patterns);61export default function (str, locale) {62 assertString(str);63 if (locale in patterns) {64 return patterns[locale].test(str);65 } else if (locale === 'any') {66 for (const key in patterns) {67 // https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes68 // istanbul ignore else69 if (patterns.hasOwnProperty(key)) {70 const pattern = patterns[key];71 if (pattern.test(str)) {72 return true;73 }74 }75 }76 return false;77 }78 throw new Error(`Invalid locale '${locale}'`);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch({ headless: false });4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.click('text=Get started');7 await page.waitForSelector('text=Get started');8 await page.fill('input[placeholder="Search..."]', 'test');9 await page.click('text=Docs');10 await page.click('text=API reference');11 await page.click('text=Page');12 await page.click('text=class: Page');13 await page.click('text=digit

Full Screen

Using AI Code Generation

copy

Full Screen

1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.type('input[name="q"]', 'Hello World')

Full Screen

Using AI Code Generation

copy

Full Screen

1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.digit('1234');7 await browser.close();8})();9const { chromium } = require('playwright');10(async () => {11 const browser = await chromium.launch();12 const context = await browser.newContext();13 const page = await context.newPage();14 await page.dblclick('#searchform');15 await browser.close();16})();17const { chromium } = require('playwright');18(async () => {19 const browser = await chromium.launch();20 const context = await browser.newContext();21 const page = await context.newPage({ viewport: { width: 500, height: 500 } });22 await page.dispatchKeyEvent('keydown', 'ArrowLeft');23 await browser.close();24})();25const { chromium } = require('playwright');26const { test } = require('@playwright/test');27test('test', async ({ page }) => {28 const element = await page.dollar('#searchform');29 console.log(element);30});31const { chromium } = require('playwright');32const { test } = require('@playwright/test');33test('test', async ({ page }) => {34 await page.dragAndDrop('#searchform', '#tsf');35});36const { chromium } = require('playwright');37const { test } = require('@

Full Screen

Using AI Code Generation

copy

Full Screen

1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.fill('input[type="text"]', 'playwright');7 await page.keyboard.press('Enter');8 await page.waitForSelector('text=Playwright');9 await page.click('text=Playwright');10 await page.waitForSelector('text=Getting Started');11 await page.click('text=Getting Started');12 await page.waitForSelector('text=Installing Playwright');13 await page.click('text=Installing Playwright');14 await page.waitForSelector('text=Playwright is a Node.js library to automate');15 await page.click('text=Playwright is a Node.js library to automate');16 await page.waitForSelector('text=Playwright is a Node.js library to automate');17 await page.click('text=Playwright is a Node.js library to automate');18 await page.waitForSelector('text=Playwright is a Node.js library to automate');19 await page.click('text=Playwright is a Node.js library to automate');20 await page.waitForSelector('text=Playwright is a Node.js library to automate');21 await page.click('text=Playwright is a Node.js library to automate');22 await page.waitForSelector('text=Playwright is a Node.js library to automate');23 await page.click('text=Playwright is a Node.js library to automate');24 await page.waitForSelector('text=Playwright is a Node.js library to automate');25 await page.click('text=Playwright is a Node.js library to automate');26 await page.waitForSelector('text=Playwright is a Node.js library to automate');27 await page.click('text=Playwright is a Node.js library to automate');28 await page.waitForSelector('text=Playwright is a Node.js library to automate');29 await page.click('text=Playwright is a Node.js library to automate');30 await page.waitForSelector('text=Playwright is a Node.js library to automate');31 await page.click('text=Playwright is a Node.js library to automate');32 await page.waitForSelector('text=Playwright is a Node.js library to automate');33 await page.click('text=Playwright is a Node.js library to automate');34 await page.screenshot({ path:

Full Screen

Using AI Code Generation

copy

Full Screen

1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.click('text=Get started');7 await page.fill('input[name="q"]', 'css');

Full Screen

Using AI Code Generation

copy

Full Screen

1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.digit('1234567890');7 await browser.close();8})();9#### playwright.executablePath()10#### playwright.selectors()11#### playwright.devices()12- returns: <[string]> Name of the browser (chromium, firefox, webkit)13#### playwright._defaultArgs(options)

Full Screen

Using AI Code Generation

copy

Full Screen

1const {chromium} = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const page = await browser.newPage();5 await page.digit(1234567890);6 await browser.close();7})();8### `page.dblclick(selector[, options])`91. Use [`page.mouse`](#pagemouse) to double click in the center of the element,10 option is set. Note that if the first click of the `dblclick()` triggers a11> **NOTE** `page.dblclick()` dispatches two `click` events and a single12await page.dblclick('text=Submit');13await page.dblclick('text=Submit', { force: true });14await page.dblclick('text=Submit', { position: { x: 20, y: 10 } });15### `page.dispatchKeyEvent(type, key[, options])`

Full Screen

Using AI Code Generation

copy

Full Screen

1const { digit } = require('playwright');2const { digit } = require('playwright');3const { digit } = require('playwright');4const { digit } = require('playwright');5const { digit } = require('playwright');6const { digit } = require('playwright');7const { digit } = require('playwright');8const { digit } = require('playwright');9const { digit } = require('playwright');

Full Screen

Using AI Code Generation

copy

Full Screen

1const {digit} = require('playwright/lib/helper');2const selector = 'input[type="text"]';3await page.fill(selector, digit(5));4const {pressKey} = require('playwright/lib/helper');5await pressKey(page, 'ArrowDown');6const {click} = require('playwright/lib/helper');7await click(page, 'text=Submit');8### click(page, selector, options)9### pressKey(page, key, options)

Full Screen

Playwright tutorial

LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful