UNPKG

5.11 kBSource Map (JSON)View Raw
1{"version":3,"sources":["../../source/findNumbers/isValidCandidate.js"],"names":["OPENING_PARENS","CLOSING_PARENS","NON_PARENS","LEAD_CLASS","PLUS_CHARS","LEAD_CLASS_LEADING","RegExp","BRACKET_PAIR_LIMIT","MATCHING_BRACKETS_ENTIRE","PUB_PAGES","isValidCandidate","candidate","offset","text","leniency","test","previousChar","lastCharIndex","length","nextChar"],"mappings":";;;;;;;;AAEA;;AACA;;AAEA;;AALA;AAUA,IAAMA,cAAc,GAAG,kBAAvB;AACA,IAAMC,cAAc,GAAG,kBAAvB;AACA,IAAMC,UAAU,eAAQF,cAAR,SAAyBC,cAAzB,MAAhB;AAEO,IAAME,UAAU,cAAOH,cAAP,SAAwBI,qBAAxB,MAAhB,C,CAEP;;;AACA,IAAMC,kBAAkB,GAAG,IAAIC,MAAJ,CAAW,MAAMH,UAAjB,CAA3B,C,CAEA;;AACA,IAAMI,kBAAkB,GAAG,iBAAM,CAAN,EAAS,CAAT,CAA3B;AAEA;;;;;;;;;;AASA,IAAMC,wBAAwB,GAAG,IAAIF,MAAJ,CAEhC,MACE,MADF,GACWN,cADX,GAC4B,KAD5B,GACoC,KADpC,GAC4CE,UAD5C,GACyD,GADzD,GAC+D,GAD/D,GACqED,cADrE,GACsF,KADtF,GAEEC,UAFF,GAEe,GAFf,GAGE,MAHF,GAGWF,cAHX,GAG4B,GAH5B,GAGkCE,UAHlC,GAG+C,IAH/C,GAGsDD,cAHtD,GAGuE,IAHvE,GAG8EM,kBAH9E,GAIEL,UAJF,GAIe,GAJf,GAKE,GAP8B,CAAjC;AAUA;;;;;;;;AAOA,IAAMO,SAAS,GAAG,kCAAlB;;AAEe,SAASC,gBAAT,CAA0BC,SAA1B,EAAqCC,MAArC,EAA6CC,IAA7C,EAAmDC,QAAnD,EACf;AACC;AACA;AACA,MAAI,CAACN,wBAAwB,CAACO,IAAzB,CAA8BJ,SAA9B,CAAD,IAA6CF,SAAS,CAACM,IAAV,CAAeJ,SAAf,CAAjD,EAA4E;AAC3E;AACA,GALF,CAOC;AACA;;;AACA,MAAIG,QAAQ,KAAK,UAAjB,EACA;AACC;AACA;AACA;AACA,QAAIF,MAAM,GAAG,CAAT,IAAc,CAACP,kBAAkB,CAACU,IAAnB,CAAwBJ,SAAxB,CAAnB,EACA;AACC,UAAMK,YAAY,GAAGH,IAAI,CAACD,MAAM,GAAG,CAAV,CAAzB,CADD,CAEC;;AACA,UAAI,qCAA2BI,YAA3B,KAA4C,wBAAcA,YAAd,CAAhD,EAA6E;AAC5E,eAAO,KAAP;AACA;AACD;;AAED,QAAMC,aAAa,GAAGL,MAAM,GAAGD,SAAS,CAACO,MAAzC;;AACA,QAAID,aAAa,GAAGJ,IAAI,CAACK,MAAzB,EACA;AACC,UAAMC,QAAQ,GAAGN,IAAI,CAACI,aAAD,CAArB;;AACA,UAAI,qCAA2BE,QAA3B,KAAwC,wBAAcA,QAAd,CAA5C,EAAqE;AACpE,eAAO,KAAP;AACA;AACD;AACD;;AAED,SAAO,IAAP;AACA","sourcesContent":["// Copy-pasted from `PhoneNumberMatcher.js`.\r\n\r\nimport { PLUS_CHARS } from '../constants'\r\nimport { limit } from './util'\r\n\r\nimport {\r\n\tisLatinLetter,\r\n\tisInvalidPunctuationSymbol\r\n} from './utf-8'\r\n\r\nconst OPENING_PARENS = '(\\\\[\\uFF08\\uFF3B'\r\nconst CLOSING_PARENS = ')\\\\]\\uFF09\\uFF3D'\r\nconst NON_PARENS = `[^${OPENING_PARENS}${CLOSING_PARENS}]`\r\n\r\nexport const LEAD_CLASS = `[${OPENING_PARENS}${PLUS_CHARS}]`\r\n\r\n// Punctuation that may be at the start of a phone number - brackets and plus signs.\r\nconst LEAD_CLASS_LEADING = new RegExp('^' + LEAD_CLASS)\r\n\r\n// Limit on the number of pairs of brackets in a phone number.\r\nconst BRACKET_PAIR_LIMIT = limit(0, 3)\r\n\r\n/**\r\n * Pattern to check that brackets match. Opening brackets should be closed within a phone number.\r\n * This also checks that there is something inside the brackets. Having no brackets at all is also\r\n * fine.\r\n *\r\n * An opening bracket at the beginning may not be closed, but subsequent ones should be. It's\r\n * also possible that the leading bracket was dropped, so we shouldn't be surprised if we see a\r\n * closing bracket first. We limit the sets of brackets in a phone number to four.\r\n */\r\nconst MATCHING_BRACKETS_ENTIRE = new RegExp\r\n(\r\n\t'^'\r\n\t+ \"(?:[\" + OPENING_PARENS + \"])?\" + \"(?:\" + NON_PARENS + \"+\" + \"[\" + CLOSING_PARENS + \"])?\"\r\n\t+ NON_PARENS + \"+\"\r\n\t+ \"(?:[\" + OPENING_PARENS + \"]\" + NON_PARENS + \"+[\" + CLOSING_PARENS + \"])\" + BRACKET_PAIR_LIMIT\r\n\t+ NON_PARENS + \"*\"\r\n\t+ '$'\r\n)\r\n\r\n/**\r\n * Matches strings that look like publication pages. Example:\r\n * <pre>Computing Complete Answers to Queries in the Presence of Limited Access Patterns.\r\n * Chen Li. VLDB J. 12(3): 211-227 (2003).</pre>\r\n *\r\n * The string \"211-227 (2003)\" is not a telephone number.\r\n */\r\nconst PUB_PAGES = /\\d{1,5}-+\\d{1,5}\\s{0,4}\\(\\d{1,4}/\r\n\r\nexport default function isValidCandidate(candidate, offset, text, leniency)\r\n{\r\n\t// Check the candidate doesn't contain any formatting\r\n\t// which would indicate that it really isn't a phone number.\r\n\tif (!MATCHING_BRACKETS_ENTIRE.test(candidate) || PUB_PAGES.test(candidate)) {\r\n\t\treturn\r\n\t}\r\n\r\n\t// If leniency is set to VALID or stricter, we also want to skip numbers that are surrounded\r\n\t// by Latin alphabetic characters, to skip cases like abc8005001234 or 8005001234def.\r\n\tif (leniency !== 'POSSIBLE')\r\n\t{\r\n\t\t// If the candidate is not at the start of the text,\r\n\t\t// and does not start with phone-number punctuation,\r\n\t\t// check the previous character.\r\n\t\tif (offset > 0 && !LEAD_CLASS_LEADING.test(candidate))\r\n\t\t{\r\n\t\t\tconst previousChar = text[offset - 1]\r\n\t\t\t// We return null if it is a latin letter or an invalid punctuation symbol.\r\n\t\t\tif (isInvalidPunctuationSymbol(previousChar) || isLatinLetter(previousChar)) {\r\n\t\t\t\treturn false\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tconst lastCharIndex = offset + candidate.length\r\n\t\tif (lastCharIndex < text.length)\r\n\t\t{\r\n\t\t\tconst nextChar = text[lastCharIndex]\r\n\t\t\tif (isInvalidPunctuationSymbol(nextChar) || isLatinLetter(nextChar)) {\r\n\t\t\t\treturn false\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\treturn true\r\n}"],"file":"isValidCandidate.js"}
\No newline at end of file