functionalscript
Version:
FunctionalScript is a purely functional subset of JavaScript
27 lines (26 loc) • 1.14 kB
JavaScript
import { msb, u8List, u8ListToVec } from "../types/bit_vec/module.f.js";
import { flatMap } from "../types/list/module.f.js";
import * as utf8 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('');
};
export const curly = (type) => (name) => (body) => [`${type} ${name}`, '{', body, '}'];
/**
* 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) => u8ListToVec(msb)(utf8.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(utf8.toCodePointList(u8List(msb)(msbV)));