functionalscript
Version:
FunctionalScript is a purely functional subset of JavaScript
27 lines (26 loc) • 1.1 kB
JavaScript
import { msb, u8List, u8ListToVec } from "../types/bit_vec/module.f.js";
import { flatMap } from "../types/list/module.f.js";
import { fromCodePointList, toCodePointList } from "./utf8/module.f.js";
import { stringToCodePointList, codePointListToString } from "./utf16/module.f.js";
export const flat = (indent) => {
const f = (prefix) => {
const g = (item) => typeof (item) === 'string' ? [`${prefix}${item}`] : f(`${prefix}${indent}`)(item);
return flatMap(g);
};
return f('');
};
const u8ListToVecMsb = u8ListToVec(msb);
/**
* Converts a string to an UTF-8, represented as an MSB first bit vector.
*
* @param s The input string to be converted.
* @returns The resulting UTF-8 bit vector, MSB first.
*/
export const msbUtf8 = (s) => u8ListToVecMsb(fromCodePointList(stringToCodePointList(s)));
/**
* Converts a UTF-8 bit vector with MSB first encoding to a string.
*
* @param msbV - The UTF-8 bit vector with MSB first encoding.
* @returns The resulting string.
*/
export const msbUtf8ToString = (msbV) => codePointListToString(toCodePointList(u8List(msb)(msbV)));