Z28149

{
    "Z1K1": "Z2",
    "Z2K1": {
        "Z1K1": "Z6",
        "Z6K1": "Z28149"
    },
    "Z2K2": {
        "Z1K1": "Z14",
        "Z14K1": "Z23984",
        "Z14K3": {
            "Z1K1": "Z16",
            "Z16K1": "Z600",
            "Z16K2": "function Z23984( Z23984K1 ) {\n\tvar input=Z23984K1;\n\tconst separators=[\" \",\"\\t\",\"/\",\"\\\\\",\"'\",'\"',\"|\"];\n\tconst prefOptions=[\"dmye\",\"dmey\",\"mdye\",\"mdey\",\"yemd\",\"eymd\",\"yedm\",\"eydm\"];\n\t\n\tfunction forceToYear(s){\n\t\tlet res=(/-?\\+?[0-9]+/.exec(s))?.[0];\n\t\tif (res==null){\n\t\t\treturn false;\n\t\t}\n\t\t//Wikifunctions.Debug(res);\n\t\tif (res.length\u003E2) return true;\n\t\tif (res[0]==\"-\" || res[0]==\"+\") return true;\n\t\treturn false;\n\t}\n\n\tfunction languageSpecificOptions(){\n\n\t\tdefineLiterals({\n\t\t\tad:1n,\n\t\t\tbc:-1n,\n\t\t\tbce:-1n,\n\t\t\tce:1n,\n\t\t},ERA,LETTER_BY_LETTER);\n\t\t\n\t\tdefineLiteralFunction(function(t,i){\n\t\t\tif (forceToYear(t[i])){\n\t\t\t\treturn [extractNumber(t[i]),1];\n\t\t\t}\n\t\t\treturn null;\n\t\t},YEAR,true);\n\t\t\n\t\tdefineLiterals({\n\t\t\tJA:1n,\n\t\t\tFE:2n,\n\t\t\tMR:3n,\n\t\t\tAP:4n,\n\t\t\tMY:5n,\n\t\t\tJN:6n,\n\t\t\tJL:7n,\n\t\t\tAU:8n,\n\t\t\tSE:9n,\n\t\t\tOC:10n,\n\t\t\tNV:11n,\n\t\t\tDE:12n,\n\t\t\tjanuary:1n,\n\t\t\tfebruary:2n,\n\t\t\tmarch:3n,\n\t\t\tapril:4n,\n\t\t\tmay:5n,\n\t\t\tjune:6n,\n\t\t\tjuly:7n,\n\t\t\taugust:8n,\n\t\t\tseptember:9n,\n\t\t\toctober:10n,\n\t\t\tnovember:11n,\n\t\t\tdecember:12n\n\t\t},MONTH);\n\t\t\n\t\tdefineConstraint([\"st\",\"nd\",\"rd\",\"th\"],DAY,AFTER);\n\t\t\n\t\tdefineLiterals({\n\t\t\ti:1n,\n\t\t\tii:2n,\n\t\t\tiii:3n,\n\t\t\tiv:4n,\n\t\t\tiiii:4n,\n\t\t\tv:5n,\n\t\t\tvi:6n,\n\t\t\tvii:7n,\n\t\t\tviii:8n,\n\t\t\tviiii:9n,\n\t\t\tix:9n,\n\t\t\tx:10n,\n\t\t\txi:11n,\n\t\t\txii:12n\n\t\t},MONTH,NORMAL);\n\t}\n\t\n\tfunction formatOutput(o){\n\t\tlet year;\n\t\tif (o[ERA]!=null){\n\t\t\tyear = o[ERA]==-1n ? -(o[YEAR]\u003C0n ? -o[YEAR] : o[YEAR])+1n : (o[YEAR]\u003C0n ? -o[YEAR] : o[YEAR]);\n\t\t} else{\n\t\t\tyear=o[YEAR];\n\t\t}\n\n\t\treturn{\n\t\t\tK3: Number(o[DAY]),\n\t\t\tK2: Number(o[MONTH])-1,\n\t\t\tK1: year\n\t\t};\n\t}\n\n\n\t//IMPORTANT: delete_chain must contain all the fields\n\tconst delete_chain=\"edmy\";\n\n\t// FIELDS\n\tconst DAY='d';\n\tconst MONTH='m';\n\tconst YEAR='y';\n\tconst ERA='e';\n\n\t// SPECIAL VALUES\n\tconst BC=-1n;\n\tconst AD=1n;\n\n\t// NUMERIC ELEMENTS SETTINGS\n\tconst numericElements=\"dmy\";\n\tconst numericElementMin={\n\t\td:1n,\n\t\tm:1n,\n\t\ty:null\n\t};\n\tconst numericElementMax={\n\t\td:31n,\n\t\tm:12n,\n\t\ty:null\n\t};\n\n\t//END OF LANGUAGE-SPECIFIC CODE\n\t\n\t//Start of general code\n\tconst BEFORE=10;\n\tconst AFTER=20;\n\tconst BEFORE_OR_AFTER=30;\n\n\tconst LENIENT=true;\n\tconst NORMAL=false;\n\tconst LETTER_BY_LETTER=Symbol();\n\t\n\tfunction MultipleMeanings(m,e){\n\t\tfor (let i=0; i\u003Cm.length; ++i){\n\t\t\tthis[m[i]]=e[i];\n\t\t}\n\t}\n\n\tfunction defineLiterals(literals,meaning,compMode=LENIENT, forced=false){\n\t\tlet bestGuess=null;\n\t\tconst compFunc= compMode==LENIENT ? lenientComp : lessLenientComp;\n\t\tfor (let m in literals){\n\t\t\tlet target=m.split(\" \");\n\t\t\tlet ok=true;\n\t\t\tfor (let t=0; t\u003Ctarget.length; ++t){\n\t\t\t\tif (elements[iAtt+t]==undefined || !compFunc(elements[iAtt+t],target[t])){\n\t\t\t\t\tok=false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (ok){\n\t\t\t\tif (bestGuess == null || bestGuess[1]\u003Ctarget.length){\n\t\t\t\t\tbestGuess = [literals[m],target.length];\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if (compMode==LETTER_BY_LETTER ){\n\t\t\t\tlet target=m.split(\"\");\n\t\t\t\ttarget.filter((l)=\u003El!=\" \");\n\t\t\t\tlet ok=true;\n\t\t\t\tfor (let t=0; t\u003Ctarget.length; ++t){\n\t\t\t\t\tif (elements[iAtt+t]==undefined || !compFunc(elements[iAtt+t],target[t])){\n\t\t\t\t\t\tok=false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (ok){\n\t\t\t\t\tif (bestGuess == null || bestGuess[1]\u003Ctarget.length){\n\t\t\t\t\t\tbestGuess = [literals[m],target.length];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\t\tif (bestGuess==null){\n\t\t\treturn false;\n\t\t}\n\n\t\tif (typeof(meaning)==\"string\"){\n\t\t\tmeaning=[meaning];\n\t\t}\n\t\tfor (let m of meaning){\n\t\t\tif (m.length\u003E1){\n\t\t\t\tlet o=new MultipleMeanings(m,bestGuess[0]);\n\t\t\t\tfor (let mAtt of m){\n\t\t\t\t\t_currGuess[mAtt]=o;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\t_currGuess[m]=bestGuess[0];\n\t\t\t}\n\t\t}\n\t\t_nSteps=bestGuess[1];\n\t\tif (forced){\n\t\t\t_forced=meaning;\n\t\t}\n\t\treturn true;\n\n\t}\n\tfunction defineLiteralFunction(f,meaning, forced=false){\n\t\tlet bestGuess=f(elements,iAtt);\n\t\tif (bestGuess==null){\n\t\t\treturn false;\n\t\t}\n\n\t\tif (typeof(meaning)==\"string\"){\n\t\t\tmeaning=[meaning];\n\t\t}\n\t\tfor (let m of meaning){\n\t\t\tif (m.length\u003E1){\n\t\t\t\tlet o=new MultipleMeanings(m,bestGuess[0]);\n\t\t\t\tfor (let mAtt of m){\n\t\t\t\t\t_currGuess[mAtt]=o;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\t_currGuess[m]=bestGuess[0];\n\t\t\t}\n\t\t}\n\t\t_nSteps=bestGuess[1];\n\t\tif (forced){\n\t\t\t_forced=meaning;\n\t\t}\n\t\treturn true;\n\t}\n\tfunction defineConstraint(literals, meaning, position, compMode=NORMAL){\n\n\t\tlet bestGuess=null;\n\t\tconst compFunc= compMode==LENIENT ? lenientComp : lessLenientComp;\n\t\tfor (let m of literals){\n\t\t\tlet target=m.split(\" \");\n\t\t\tlet ok=true;\n\t\t\tfor (let t=0; t\u003Ctarget.length; ++t){\n\t\t\t\tif (elements[iAtt+t]==undefined || !compFunc(elements[iAtt+t],target[t])){\n\t\t\t\t\tok=false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (ok){\n\t\t\t\tif (bestGuess == null || bestGuess\u003Ctarget.length){\n\t\t\t\t\tbestGuess = target.length;\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\t\tif (bestGuess==null){\n\t\t\treturn false;\n\t\t}\n\t\t\n\n\t\tif (typeof(meaning)==\"string\"){\n\t\t\tmeaning=[meaning];\n\t\t}\n\t\t\n\t\t_constraintsList.push({pos:possibilities.length, meaning:meaning, type: position});\n\n\t\t_nSteps=bestGuess;\n\t\t_isToBeIgnored=true;\n\t\treturn true;\n\t}\n\tfunction defineConstraintFunction(f,meaning,position){\n\n\t\tlet bestGuess=f(elements,iAtt);\n\t\tif (bestGuess==null){\n\t\t\treturn false;\n\t\t}\n\n\t\tif (typeof(meaning)==\"string\"){\n\t\t\tmeaning=[meaning];\n\t\t}\n\t\t_constraintsList.push({pos:possibilities.length, meaning:meaning, type: position});\n\t\t_nSteps=bestGuess;\n\t\t_isToBeIgnored=true;\n\t\treturn true;\n\t}\n\n\tfunction lenientComp(a,b){\n\t\ta=a.toLowerCase().replaceAll(/,|\\.|;|:/g,\"\");\n\t\tb=b.toLowerCase();\n\t\tif (a==\"\") return false;\n\t\treturn a==b.substring(0,a.length);\n\t}\n\n\tfunction lessLenientComp(a,b){\n\t\ta=a.toLowerCase().replaceAll(/,|\\.|;|:/g,\"\");\n\t\tb=b.toLowerCase();\n\t\tif (a==\"\") return false;\n\t\treturn a==b;\n\t}\n\n\tfunction extractNumber(s){\n\t\tlet res=/-?[0-9]+/.exec(s);\n\t\tif (res!=null){\n\t\t\treturn BigInt(res);\n\t\t}\n\t\treturn null;\n\t}\n\n\tfunction isLeapYear(y){\n\t\treturn ((y \u0026 3n) == 0n \u0026\u0026\n\t\t\t((y % 25n) != 0n \n\t\t\t|| (y \u0026 15n) == 0n));\n\t}\n\t\n\tfunction lengthOfMonth(m,isLeap){\n\t\tif (m==1) \n\t\t\treturn isLeap ? 29 : 28;\n\t\telse \n\t\t\treturn 30+(1+m+Math.floor(m/7))%2;\n\t}\n\t\n\tfunction isValidDate(d){\n\t\tif (d==null || d.K1==undefined || d.K2==undefined || d.K3==undefined){\n\t\t\treturn false;\n\t\t}\n\t\tlet isLeap=isLeapYear(d.K1);\n\t\tif (d.K2\u003C0 || d.K2\u003E11){\n\t\t\treturn false;\n\t\t}\n\t\tif (d.K3\u003C1 || d.K3\u003ElengthOfMonth(d.K2,isLeap)){\n\t\t\treturn false;\n\t\t}\n\t\treturn true;\n\t}\n\n\t\n\t/**************** START TOKENISATION ***********************/\n\n\tinput=input.replaceAll(/-|—|‒|–|—|⸺|⸺/g,\"-\");\n\n\n\tvar elements=[\"\"];\n\tfor (let i=0; i\u003Cinput.length;){\n\t\tlet s=separators.find((v)=\u003E input.substring(i,i+v.length)==v);\n\t\tif (s!==undefined){\n\t\t\tif(elements[elements.length-1]!=\"\")\n\t\t\t\telements.push(\"\");\n\t\t\ti+=s.length;\n\t\t\tcontinue;\n\t\t}\n\t\telements[elements.length-1]+=input[i++];\n\t}\n\tif(elements[elements.length-1]==\"\") elements.pop();\n\n\n\tlet elSeparato=[];\n\tfor (let i=0; i\u003Celements.length; ++i){\n\t\t//Separate numbers from text\n\t\tfor (let e of elements[i].matchAll(/((?:(?\u003C=^|\\s)[\\-\\+])?[0-9]+)?(?:\\-?([^0-9\\-]+))?/g)){\n\t\t\telSeparato.push(e.splice(1))\n\t\t}\n\t}\n\telSeparato=elSeparato.flat();\n\telements=elSeparato.filter((v)=\u003E(v!=null \u0026\u0026 v!=\"\"));\n\n\t// Wikifunctions.Debug(\"elements:\");\n\t// Wikifunctions.Debug(elements);\n\n\t/**************** END TOKENISATION ***********************/\n\t/**************** START FINDING POSSIBILITIES ***********************/\n\tvar possibilities=[];\n\n\tvar _constraintsList=[];\n\tvar _nSteps=1;\n\n\tfor(var iAtt=0; iAtt\u003Celements.length; iAtt+=_nSteps){\n\t\t_nSteps=1;\n\t\tlet n=extractNumber(elements[iAtt]);\n\t\tvar _currGuess={};\t//Object containing all the current guesses\n\t\tvar _forced=null;\n\t\tvar _isToBeIgnored=false;\n\n\t\t//////////\n\n\t\tlanguageSpecificOptions();\n\n\t\tfor (let i of numericElements){\n\t\t\tif ((numericElementMin[i]==null || n\u003E=numericElementMin[i]) \u0026\u0026 (numericElementMax[i]==null || n\u003C=numericElementMax[i])){\n\t\t\t\t_currGuess[i]??=n;\n\t\t\t}\n\t\t}\n\n\n\n\t\tif (_forced!=null){\n\t\t\tfor (let j in _currGuess){\n\t\t\t\tif (_forced.find((v)=\u003E(v==j)) == undefined){\n\t\t\t\t\t_currGuess[j]=null;\t\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (!_isToBeIgnored)\n\t\t\tpossibilities.push(_currGuess);\n\t}\n\t\n\n\tif (_constraintsList.length\u003E0){\n\t\tfor (let pAtt=0; pAtt\u003C possibilities.length; ++pAtt){\n\t\t\tfor (let c of _constraintsList){\n\t\t\t\tif ((c.type==AFTER \u0026\u0026 pAtt!=c.pos-1) ||\n\t\t\t\t    (c.type==BEFORE \u0026\u0026 pAtt!=c.pos) ||\n\t\t\t\t    (c.type==BEFORE_OR_AFTER \u0026\u0026 pAtt!=c.pos-1 \u0026\u0026 pAtt!=c.pos)){\n\t\t\t\t\tfor(let m of c.meaning){\n\t\t\t\t\t\tpossibilities[pAtt][m]=null;\t\n\t\t\t\t\t}\n\t\t\t\t} else if ((c.type==AFTER \u0026\u0026 pAtt==c.pos-1) ||\n\t\t\t\t\t(c.type==BEFORE \u0026\u0026 pAtt==c.pos)){\n\t\t\t\t\tfor (let m in possibilities[pAtt]){\n\t\t\t\t\t\tif (c.meaning.find((x)=\u003E x==m)==undefined){\n\t\t\t\t\t\t\tpossibilities[pAtt][m]=null;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t/**************** END FINDING POSSIBILITIES ***********************/\n\n\n\tpossibilities=possibilities.filter((v)=\u003E(Object.entries(v).filter((x)=\u003Ex[1]!=null).length\u003E0));\n\n\t//remove elements that are not present\n\tlet lettersToRemove=\"\";\n\tif(delete_chain.length\u003Epossibilities.length){\n\t\tfor (let i of delete_chain){\n\t\t\tif (possibilities.find((p)=\u003Ep[i]!=null)==undefined){\n\t\t\t\tlettersToRemove+=i;\n\t\t\t}\n\t\t}\n\t}\n\n\t//save tokens that have only one possibility\n\tlet nLettersToMantain=0;\n\tfor (let p of possibilities){\n\t\tlet currn=1;\n\t\tfor (let e in p){\n\t\t\tif (p[e] instanceof MultipleMeanings){\n\t\t\t\tcurrn=Math.max(currn,Object.entries(p[e]).length);\n\t\t\t}\n\t\t}\n\t\tnLettersToMantain+=currn;\n\t}\n\t\n\tlet safeLetters=\"\";\n\tlet compulsoryLetters=\"\";\n\tif(delete_chain.length\u003EnLettersToMantain){\n\t\tfor (let p of possibilities){\n\t\t\tif (Object.entries(p).filter((x)=\u003Ex[1]!=null).length ==1){\n\t\t\t\tsafeLetters+=Object.entries(p).filter((x)=\u003Ex[1]!=null)[0][0];\n\t\t\t}\n\t\t}\n\t}\n\n\t\n\tfor (let c of _constraintsList){\n\t\tif (c.meaning.length!==1){\n\t\t\tcontinue;\n\t\t}\n\t\tif(!compulsoryLetters.includes(c.meaning[0])){\n\t\t\tcompulsoryLetters+=c.meaning[0];\n\t\t}\n\t}\n\n\n\t\n\t\t/**************** CHECK OF ALL ORDERS ***********************/\n\t\n\tfor (let i=0; i\u003CprefOptions.length; ++i){\n\t\tlet actPref=prefOptions[i];\n\t\tlet elementsFound={};\n\t\t\n\t\t\n\t\tlet localLettersToRemove=lettersToRemove;\n\t\t//remove elements according to the deletion chain\n\t\tfor (let i=0; i\u003Cdelete_chain.length; ++i){\n\t\t\tif (actPref.indexOf(delete_chain[i])===-1){\n\t\t\t\tlocalLettersToRemove+=delete_chain[i];\n\t\t\t}\n\t\t}\n\t\tfor(let i=0; delete_chain.length-localLettersToRemove.length\u003EnLettersToMantain \u0026\u0026 i\u003Cdelete_chain.length; ++i){\n\t\t\tif (localLettersToRemove.indexOf(delete_chain[i])== -1 \u0026\u0026 safeLetters.indexOf(delete_chain[i])==-1){\n\t\t\t\tlocalLettersToRemove+= delete_chain[i];\n\t\t\t}\n\t\t}\n\t\t\n\t\tfor (let l of localLettersToRemove){\n\t\t\tactPref=actPref.replace(l,\"\");\n\t\t}\n\t\tlet actPrefIt=actPref[Symbol.iterator]();\n\t\tlet currActPrefObj=actPrefIt.next();\n\t\t\n\t\tlet ok=true;\n\t\tfor (let j=0; j\u003Cpossibilities.length \u0026\u0026 !currActPrefObj.done; ++j){\n\t\t\twhile (elementsFound[currActPrefObj.value]!=undefined){\n\t\t\t\tcurrActPrefObj=actPrefIt.next();\n\t\t\t\tif (currActPrefObj.done){\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (currActPrefObj.done){\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif (possibilities[j]?.[currActPrefObj.value] instanceof MultipleMeanings){\n\t\t\t\tfor (let m in possibilities[j][currActPrefObj.value]){\n\t\t\t\t\telementsFound[m]=possibilities[j][currActPrefObj.value][m];\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\telementsFound[currActPrefObj.value]=possibilities[j]?.[currActPrefObj.value];\n\t\t\t\tif (elementsFound[currActPrefObj.value] == null){\n\t\t\t\t\tok=false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tcurrActPrefObj=actPrefIt.next();\n\t\t}\n\t\tfor (let f of safeLetters){\n\t\t\tif (actPref.includes(f) \u0026\u0026 elementsFound[f]==null){\n\t\t\t\tok=false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tfor (let f of compulsoryLetters){\n\t\t\tif (elementsFound[f]==null){\n\t\t\t\tok=false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (ok){\n\t\t\tlet r= formatOutput(elementsFound);\n\t\t\tif (isValidDate(r)){\n\t\t\t\treturn r;\n\t\t\t}\n\t\t}\n\t}\n\t\n\tWikifunctions.Error( 'Z28131', [ input, \"en\" ] );\n\n\n}"
        }
    },
    "Z2K3": {
        "Z1K1": "Z12",
        "Z12K1": [
            "Z11",
            {
                "Z1K1": "Z11",
                "Z11K1": "Z1787",
                "Z11K2": "leggi Data gregoriana, en (gma), errore, aggiornat"
            },
            {
                "Z1K1": "Z11",
                "Z11K1": "Z1002",
                "Z11K2": "read Gregorian Date,en (dmy),custom errors,updated"
            }
        ]
    },
    "Z2K4": {
        "Z1K1": "Z32",
        "Z32K1": [
            "Z31"
        ]
    },
    "Z2K5": {
        "Z1K1": "Z12",
        "Z12K1": [
            "Z11"
        ]
    }
}