decline-ts
Version:
Composable command-line parser for TypeScript - a (partial) porting of Scala decline using fp-ts
22 lines (21 loc) • 1.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StringUtils = void 0;
const fp_ts_1 = require("fp-ts");
const function_1 = require("fp-ts/function");
var StringUtils;
(function (StringUtils) {
StringUtils.isEmpty = (str) => str === '';
StringUtils.isNonEmpty = (str) => !StringUtils.isEmpty(str);
const margin = /^[^\n\S]*\|/gm;
StringUtils.stripMargins = (str) => str.replace(margin, '');
function mkString(startOrSep, sep, end) {
return list => sep !== undefined && end !== undefined
? `${startOrSep}${list.join(sep)}${end}`
: list.join(startOrSep);
}
StringUtils.mkString = mkString;
const matcher = (regex, f) => (str) => function_1.pipe(str.match(regex), fp_ts_1.option.fromNullable, fp_ts_1.option.chain(f));
StringUtils.matcher1 = (regex) => matcher(regex, ([, a]) => fp_ts_1.option.fromNullable(a));
StringUtils.matcher2 = (regex) => matcher(regex, ([, _1, _2]) => function_1.pipe(fp_ts_1.option.fromNullable(_1), fp_ts_1.option.bindTo('a'), fp_ts_1.option.bind('b', () => fp_ts_1.option.fromNullable(_2)), fp_ts_1.option.map(({ a, b }) => [a, b])));
})(StringUtils = exports.StringUtils || (exports.StringUtils = {}));