gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 1.09 kB
JavaScript
/*! Copyright 2023-2025 the gnablib contributors MPL-1.1 */
import{escape as t}from"../regexp/index.js";export function splitChars(t){return Array.from(t)}export function splitLen(t,e){const r=[];let n="",o=0;for(const l of t)n+=l,o++,o==e&&(r.push(n),n="",o=0);return o>0&&r.push(n),r}export function reverse(t){return splitChars(t).reverse().join("")}export function padStart(t,e,r=" "){const n=e-t.length;if(n<=0||0===r.length)return t;const o=(r.length+n-1)/r.length;let l=r.repeat(o).concat(t);const i=l.length-e;return i>0&&(l=l.substring(i)),l}export function filter(e,r,n){return"string"==typeof r&&(r=new RegExp("["+t(r)+"]+","g")),n=null!=n?n:"",e.replace(r,n)}export function ctEq(t,e){if(t.length!=e.length)return!1;let r=0;for(let n=0;n<t.length;n++)r|=t.charCodeAt(n)^e.charCodeAt(n);return 0===r}export function ctSelect(t,e,r){const n=(0|r)-1,o=splitChars(t),l=splitChars(e);if(o.length!=l.length)throw new Error("Inputs are of different length");const i=[];for(let t=0;t<o.length;t++)i.push(~n&o[t].codePointAt(0)|n&l[t].codePointAt(0));return String.fromCodePoint(...i)}