@mojir/lits
Version:
Lits is a pure functional programming language implemented in TypeScript
3 lines (2 loc) • 12.6 kB
JavaScript
function t(t){if(!t.position||!t.code)return"";const e=t.position.column-1,r=t.code.length-e-1;return`${" ".repeat(Math.max(e,0))}^${" ".repeat(Math.max(r,0))}`}class e extends Error{sourceCodeInfo;shortMessage;constructor(r,n){const i=r instanceof Error?r.message:`${r}`;super(function(e,r){if(!r)return e;const n=`${r.position.line}:${r.position.column}`;return`${e}${r.filePath?`\n${r.filePath}:${n}`:`\nLocation ${n}`}\n${r.code}\n${t(r)}`}(i,n)),this.shortMessage=i,this.sourceCodeInfo=n,Object.setPrototypeOf(this,e.prototype),this.name="LitsError"}getCodeMarker(){return this.sourceCodeInfo&&t(this.sourceCodeInfo)}}const r={Number:1,String:2,NormalExpression:3,SpecialExpression:4,UserDefinedSymbol:5,NormalBuiltinSymbol:6,SpecialBuiltinSymbol:7,ReservedSymbol:8,Binding:9,Spread:10},n=new Set(Object.values(r));const i=new Set(["UserDefined","Partial","Comp","Constantly","Juxt","Complement","EveryPred","SomePred","Fnull","Builtin","SpecialBuiltin","NativeJsFunction","Module"]);function o(t){return null!==t&&"object"==typeof t&&("^^fn^^"in t&&"functionType"in t&&("string"==typeof(e=t.functionType)&&i.has(e)));var e}function s(t){return!(!Array.isArray(t)||t.length<2)&&("number"==typeof(e=t[0])&&n.has(e));var e}function a(t){return o(t)?`<function ${t.name||"λ"}>`:s(t)?`${e=t[0],Object.keys(r).find(t=>r[t]===e)}-node`:null===t?"null":"object"==typeof t&&t instanceof RegExp?`${t}`:"object"==typeof t&&t instanceof Error?t.toString():JSON.stringify(t);var e}function p(t,e){return t?.sourceCodeInfo??e}function l(t,r){return function(t,r){if(!function(t){return void 0!==t}(t))throw new e("Unexpected undefined",p(t,r))}(t,r),t}function g(t,r,n){return new e(`Expected ${t}, got ${a(r)}.`,p(r,n))}function m(t,r,n={}){if(!function(t,e={}){return!("number"!=typeof t||Number.isNaN(t)||e.integer&&!Number.isInteger(t)||e.finite&&!Number.isFinite(t)||e.zero&&0!==t||e.nonZero&&0===t||e.positive&&t<=0||e.negative&&t>=0||e.nonPositive&&t>0||e.nonNegative&&t<0||"number"==typeof e.gt&&t<=e.gt||"number"==typeof e.gte&&t<e.gte||"number"==typeof e.lt&&t>=e.lt||"number"==typeof e.lte&&t>e.lte)}(t,n))throw new e(`Expected ${function(t){if(t.zero)return"zero";const e=function(t){return t.positive?"positive":t.negative?"negative":t.nonNegative?"non negative":t.nonPositive?"non positive":t.nonZero?"non zero":""}(t),r=t.integer?"integer":"number",n=t.finite?"finite":"",i=function(t){return"number"!=typeof t.gt&&"number"!=typeof t.gte||"number"!=typeof t.lt&&"number"!=typeof t.lte?"number"==typeof t.gt||"number"==typeof t.gte?"number"==typeof t.gt?`n > ${t.gt}`:`n >= ${t.gte}`:"number"==typeof t.lt||"number"==typeof t.lte?"number"==typeof t.lt?`n < ${t.lt}`:`n <= ${t.lte}`:"":`${"number"==typeof t.gt?`${t.gt} < n `:`${t.gte} <= n `}${"number"==typeof t.lt?`< ${t.lt}`:`<= ${t.lte}`}`}(t);return[e,n,r,i].filter(t=>!!t).join(" ")}(n)}, got ${a(t)}.`,p(t,r))}function c(t,e,r={}){if(!function(t,e={}){return!("string"!=typeof t||e.nonEmpty&&0===t.length||e.char&&1!==t.length)}(t,r))throw g(""+(r.nonEmpty?"non empty string":r.char?"character":"string"),t,e)}function u(t,e){return function(t,e){if(!function(t){return"string"==typeof t||"number"==typeof t}(t))throw g("string or number",t,e)}(t,e),t}function d(t){return{min:t,max:t}}const f={"string-repeat":{evaluate:([t,e],r)=>(c(t,r),m(e,r,{integer:!0,nonNegative:!0}),t.repeat(e)),arity:d(2),docs:{category:"string",returns:{type:"number"},args:{a:{type:"string"},b:{type:"integer"},s:{type:"string"},n:{type:"integer"}},variants:[{argumentNames:["s","n"]}],description:"Repeates $s $n times.",seeAlso:["str","repeat"],examples:['let { string-repeat } = import(string);\n"*" string-repeat 10','let { string-repeat } = import(string);\nstring-repeat("*", 10)','let { string-repeat } = import(string);\nstring-repeat("***", 0)']}},"from-char-code":{evaluate:([t],r)=>{m(t,r,{finite:!0});const n=function(t){return Math.max(0,Math.ceil(t))}(t);try{return String.fromCodePoint(n)}catch(t){throw new e(t,r)}},arity:d(1),docs:{category:"string",returns:{type:"string"},args:{code:{type:"number"}},variants:[{argumentNames:["code"]}],description:"Return character for code point $code.",seeAlso:["string.to-char-code"],examples:["let { from-char-code } = import(string);\nfrom-char-code(65)","let { from-char-code } = import(string);\nfrom-char-code(0)"]}},"to-char-code":{evaluate:([t],e)=>(c(t,e,{nonEmpty:!0}),l(t.codePointAt(0),e)),arity:d(1),docs:{category:"string",returns:{type:"number"},args:{c:{type:"string"}},variants:[{argumentNames:["c"]}],description:"Return code point for first character in $c.",seeAlso:["string.from-char-code"],examples:['let { to-char-code } = import(string);\nto-char-code("A")','let { to-char-code } = import(string);\nto-char-code("Albert")']}},"trim-left":{evaluate:([t],e)=>(c(t,e),t.replace(/^\s+/,"")),arity:d(1),docs:{category:"string",returns:{type:"string"},args:{s:{type:"string"}},variants:[{argumentNames:["s"]}],description:"Returns a new string with leading whitespaces removed.",seeAlso:["trim","string.trim-right"],examples:['let { trim-left } = import(string);\ntrim-left(" Albert ")','let { trim-left } = import(string);\ntrim-left(" ")','let { trim-left } = import(string);\ntrim-left("")']}},"trim-right":{evaluate:([t],e)=>(c(t,e),t.replace(/\s+$/,"")),arity:d(1),docs:{category:"string",returns:{type:"string"},args:{s:{type:"string"}},variants:[{argumentNames:["s"]}],description:"Returns a new string with trailing whitespaces removed.",seeAlso:["trim","string.trim-left"],examples:['let { trim-right } = import(string);\ntrim-right(" Albert ")','let { trim-right } = import(string);\ntrim-right(" ")','let { trim-right } = import(string);\ntrim-right("")']}},"split-lines":{evaluate:([t],e)=>(c(t,e),t.split(/\r\n|\n|\r/).filter(t=>""!==t)),arity:d(1),docs:{category:"string",returns:{type:"string"},args:{s:{type:"string"}},variants:[{argumentNames:["s"]}],description:"Divides $s into an array of substrings, each representing a line.",seeAlso:["split"],examples:['let { split-lines } = import(string);\nsplit-lines("Albert\nMojir\n")','let { split-lines } = import(string);\nsplit-lines("Albert\n\nMojir")','let { split-lines } = import(string);\nsplit-lines("Albert\nMojir\n\n")','let { split-lines } = import(string);\nsplit-lines("")']}},"pad-left":{evaluate:([t,e,r],n)=>(c(t,n),m(e,n,{integer:!0}),void 0!==r&&c(r,n),t.padStart(e,r)),arity:{min:2,max:3},docs:{category:"string",returns:{type:"string"},args:{a:{type:"string"},b:{type:"integer"},s:{type:"string"},length:{type:"integer"},padString:{type:"string"}},variants:[{argumentNames:["s","length"]},{argumentNames:["s","length","padString"]}],description:"Pads from the start of $s with `padString` (multiple times, if needed) until the resulting string reaches the given $length.",seeAlso:["string.pad-right"],examples:['let { pad-left } = import(string);\n"Albert" pad-left 20','let { pad-left } = import(string);\npad-left("Albert", 20)','let { pad-left } = import(string);\npad-left("Albert", 20, "-*-")','let { pad-left } = import(string);\npad-left("Albert", 5)','let { pad-left } = import(string);\npad-left("Albert", -1)']}},"pad-right":{evaluate:([t,e,r],n)=>(c(t,n),m(e,n,{integer:!0}),void 0!==r&&c(r,n),t.padEnd(e,r)),arity:{min:2,max:3},docs:{category:"string",returns:{type:"string"},args:{a:{type:"string"},b:{type:"integer"},s:{type:"string"},length:{type:"integer"},padString:{type:"string"}},variants:[{argumentNames:["s","length"]},{argumentNames:["s","length","padString"]}],description:"Pads from the start of $s with `padString` (multiple times, if needed) until the resulting string reaches the given `length`.",seeAlso:["string.pad-left"],examples:['let { pad-right } = import(string);\n"Albert" pad-right 20','let { pad-right } = import(string);\npad-right("Albert", 20)','let { pad-right } = import(string);\npad-right("Albert", 20, "-*-")','let { pad-right } = import(string);\npad-right("Albert", 5)','let { pad-right } = import(string);\npad-right("Albert", -1)']}},template:{evaluate:([t,...e],r)=>{c(t,r),function(t,e){if(!Array.isArray(t))throw g("array",t,e)}(e,r);const n=t.split("||||");if(n.length<=1)return b(n[0],e,r);{const t=e[0];m(t,r,{integer:!0,nonNegative:!0});const i=[`${t}`,...e.slice(1)];if(2===n.length){return b(n[1===t?0:1],i,r)}return b(n[Math.min(t,n.length-1)],i,r)}},arity:{min:1,max:10},docs:{category:"string",returns:{type:"string"},args:{s:{type:"string"},params:{type:"any",rest:!0}},variants:[{argumentNames:["s","params"]}],description:"Applies placeholders to a string. Support for basic pluralization - see examples. If pluralization is used, first placeholder must be a number.",seeAlso:["str"],examples:['let { template } = import(string);\ntemplate("Hi, $1 and $2", "Carl", "Larry")','let { template } = import(string);\ntemplate("Hi $1, $2, $3, $4, $5, $6, $7, $8 and $9", "A", "B", "C", "D", "E", "F", "G", "H", "I")','let { template } = import(string);\ntemplate("$1 book||||$1 books", 0)','let { template } = import(string);\ntemplate("$1 book||||$1 books", 1)','let { template } = import(string);\ntemplate("$1 book||||$1 books", 2)','let { template } = import(string);\ntemplate("No book||||$1 book||||$1 books", 0)','let { template } = import(string);\ntemplate("No book||||$1 book||||$1 books", 1)','let { template } = import(string);\ntemplate("No book||||$1 book||||$1 books", 10)','let { template } = import(string);\ntemplate("No book||||One book||||Two books||||Three books||||$1 books", 0)','let { template } = import(string);\ntemplate("No book||||One book||||Two books||||Three books||||$1 books", 1)','let { template } = import(string);\ntemplate("No book||||One book||||Two books||||Three books||||$1 books", 2)','let { template } = import(string);\ntemplate("No book||||One book||||Two books||||Three books||||$1 books", 3)','let { template } = import(string);\ntemplate("No book||||One book||||Two books||||Three books||||$1 books", 4)'],hideOperatorForm:!0}},"encode-base64":{evaluate:([t],e)=>(c(t,e),btoa(encodeURIComponent(t).replace(/%([0-9A-F]{2})/g,(t,e)=>String.fromCharCode(Number.parseInt(e,16))))),arity:d(1),docs:{category:"string",returns:{type:"string"},args:{s:{type:"string"}},variants:[{argumentNames:["s"]}],description:"Returns a Base64 encoded string from $s.",seeAlso:["string.decode-base64"],examples:['let { encode-base64 } = import(string);\nencode-base64("Albert")']}},"decode-base64":{evaluate:([t],r)=>{c(t,r);try{return decodeURIComponent(Array.prototype.map.call(atob(t),t=>`%${`00${t.charCodeAt(0).toString(16)}`.slice(-2)}`).join(""))}catch(t){throw new e(t,r)}},arity:d(1),docs:{category:"string",returns:{type:"string"},args:{base64string:{type:"string"}},variants:[{argumentNames:["base64string"]}],description:"Returns a Base64 decoded string from $base64string.",seeAlso:["string.encode-base64"],examples:['let { decode-base64 } = import(string);\ndecode-base64("QWxiZXJ0IPCfkLs=")']}},"encode-uri-component":{evaluate:([t],e)=>(c(t,e),encodeURIComponent(t)),arity:d(1),docs:{category:"string",returns:{type:"string"},args:{s:{type:"string"}},variants:[{argumentNames:["s"]}],description:"Returns an escaped `URI` string.",seeAlso:["string.decode-uri-component"],examples:['let { encode-uri-component } = import(string);\nencode-uri-component("Hi everyone!?")']}},"decode-uri-component":{evaluate:([t],r)=>{c(t,r);try{return decodeURIComponent(t)}catch(t){throw new e(t,r)}},arity:d(1),docs:{category:"string",returns:{type:"string"},args:{s:{type:"string"}},variants:[{argumentNames:["s"]}],description:"Returns an un-escaped `URI` string.",seeAlso:["string.encode-uri-component"],examples:['let { decode-uri-component } = import(string);\ndecode-uri-component("Hi%20everyone!%3F%20%F0%9F%91%8D")']}},capitalize:{evaluate:([t],e)=>(c(t,e),t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()),arity:d(1),docs:{category:"string",returns:{type:"string"},args:{s:{type:"string"}},variants:[{argumentNames:["s"]}],description:"Returns $s with the first character converted to uppercase and the rest to lowercase.",seeAlso:["lower-case","upper-case"],examples:['let { capitalize } = import(string);\ncapitalize("albert")','let { capitalize } = import(string);\ncapitalize("ALBERT")','let { capitalize } = import(string);\ncapitalize("aLBERT")','let { capitalize } = import(string);\ncapitalize("")']}}},y=/\$\$/g;function b(t,e,r){for(let n=0;n<9;n+=1){const i=new RegExp(`(\\$\\$|[^$]|^)\\$${n+1}`,"g");if(i.test(t)){const o=u(e[n],r);t=t.replace(i,`$1${o}`)}}return t=t.replace(y,"$")}const h={name:"string",functions:f};export{h as stringUtilsModule};
//# sourceMappingURL=string.esm.js.map