monkey-front-core 0.0.514 → 0.0.516

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -291,23 +291,151 @@ class MonkeyEcxUtils {
291
291
  return dvCalc === dgv;
292
292
  }
293
293
  static isValidRFC(document) {
294
- const regex = /^[A-Z]{4}[0-9]{6}[A-Z0-9]{3}$/;
295
- if (!regex.test(document)) {
296
- return false;
297
- }
298
- const year = parseInt(document.substr(4, 2));
299
- const month = parseInt(document.substr(6, 2));
300
- const day = parseInt(document.substr(8, 2));
301
- if (year < 0 || year > 99) {
302
- return false;
303
- }
304
- if (month < 1 || month > 12) {
305
- return false;
294
+ const RFC_REGEXP = /^([A-ZÑ\x26]{3,4})([0-9]{6})([A-Z0-9]{3})$/;
295
+ const SPECIAL_CASES = {
296
+ 'XEXX010101000': 'foreign',
297
+ 'XAXX010101000': 'generic'
298
+ };
299
+ const FORBIDDEN_WORDS = [
300
+ "BUEI",
301
+ "BUEY",
302
+ "CACA",
303
+ "CACO",
304
+ "CAGA",
305
+ "CAGO",
306
+ "CAKA",
307
+ "CAKO",
308
+ "COGE",
309
+ "COJA",
310
+ "COJE",
311
+ "COJI",
312
+ "COJO",
313
+ "CULO",
314
+ "FETO",
315
+ "GUEY",
316
+ "JOTO",
317
+ "KACA",
318
+ "KACO",
319
+ "KAGA",
320
+ "KAGO",
321
+ "KOGE",
322
+ "KOJO",
323
+ "KAKA",
324
+ "KULO",
325
+ "MAME",
326
+ "MAMO",
327
+ "MEAR",
328
+ "MEAS",
329
+ "MEON",
330
+ "MION",
331
+ "MOCO",
332
+ "MULA",
333
+ "PEDA",
334
+ "PEDO",
335
+ "PENE",
336
+ "PUTA",
337
+ "PUTO",
338
+ "QULO",
339
+ "RATA",
340
+ "RUIN"
341
+ ];
342
+ const VALUES_MAP = {
343
+ '0': 0,
344
+ '1': 1,
345
+ '2': 2,
346
+ '3': 3,
347
+ '4': 4,
348
+ '5': 5,
349
+ '6': 6,
350
+ '7': 7,
351
+ '8': 8,
352
+ '9': 9,
353
+ 'A': 10,
354
+ 'B': 11,
355
+ 'C': 12,
356
+ 'D': 13,
357
+ 'E': 14,
358
+ 'F': 15,
359
+ 'G': 16,
360
+ 'H': 17,
361
+ 'I': 18,
362
+ 'J': 19,
363
+ 'K': 20,
364
+ 'L': 21,
365
+ 'M': 22,
366
+ 'N': 23,
367
+ '&': 24,
368
+ 'O': 25,
369
+ 'P': 26,
370
+ 'Q': 27,
371
+ 'R': 28,
372
+ 'S': 29,
373
+ 'T': 30,
374
+ 'U': 31,
375
+ 'V': 32,
376
+ 'W': 33,
377
+ 'X': 34,
378
+ 'Y': 35,
379
+ 'Z': 36,
380
+ ' ': 37,
381
+ 'Ñ': 38
382
+ };
383
+ const getScore = (doc) => doc.split('').reverse().reduce((sum, char, i) => {
384
+ const index = i + 2;
385
+ const value = VALUES_MAP[char] || 0;
386
+ return sum + value * index;
387
+ }, 0);
388
+ const parseInput = (doc) => {
389
+ return String(doc)
390
+ .trim()
391
+ .toUpperCase()
392
+ .replace(/[^0-9A-ZÑ\x26]/g, '');
393
+ };
394
+ const validateDate = (doc) => {
395
+ const dateStr = doc.slice(0, -3).slice(-6);
396
+ const year = dateStr.slice(0, 2);
397
+ const month = dateStr.slice(2, 4);
398
+ const day = dateStr.slice(4, 6);
399
+ const date = new Date(`20${year}-${month}-${day}`);
400
+ return !isNaN(date.getTime());
401
+ };
402
+ const validateVerificationDigit = (doc) => {
403
+ const rfc = doc.length === 12 ? ` ${doc}` : doc;
404
+ const base = rfc.slice(0, -1);
405
+ const score = getScore(base);
406
+ const mod = (11000 - score) % 11;
407
+ if (mod === 11)
408
+ return '0';
409
+ if (mod === 10)
410
+ return 'A';
411
+ return String(mod);
412
+ };
413
+ const isSpecialCase = (doc) => doc in SPECIAL_CASES;
414
+ const hasForbiddenWords = (doc) => {
415
+ const prefix = (doc || '').slice(0, 4);
416
+ return FORBIDDEN_WORDS.includes(prefix);
417
+ };
418
+ try {
419
+ const input = parseInput(document);
420
+ if (isSpecialCase(input))
421
+ return false;
422
+ const hasValidFormat = RFC_REGEXP.test(input);
423
+ if (!hasValidFormat)
424
+ return false;
425
+ const hasValidDate = validateDate(input);
426
+ if (!hasValidDate)
427
+ return false;
428
+ const hasValidDigit = validateVerificationDigit(input);
429
+ if (!hasValidDigit)
430
+ return false;
431
+ if (hasForbiddenWords(input))
432
+ return false;
433
+ return true;
306
434
  }
307
- if (day < 1 || day > 31) {
308
- return false;
435
+ catch (e) {
436
+ //not to do
309
437
  }
310
- return true;
438
+ return false;
311
439
  }
312
440
  static isValidUrl(txt) {
313
441
  const regex = /(https?:\/\/(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9])(:?\d*)\/?([a-z_\\/0-9\-#.]*)\??([a-z_\\/0-9\-#=&]*)/g;