@prismicio/types-internal
Version:
Prismic types for Custom Types and Prismic Data
25 lines (24 loc) • 891 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const Either_1 = require("fp-ts/Either");
const function_1 = require("fp-ts/function");
const t = (0, tslib_1.__importStar)(require("io-ts"));
const PixelsRegex = /^([0-9]+)px$/;
/**
* A codec that succeeds if a string representing pixels (eg: "200px") can be
* parsed to an integer
*/
exports.default = new t.Type("IntFromPixels", t.Int.is, (u, c) => (0, function_1.pipe)(t.string.validate(u, c), (0, Either_1.chain)((strPixels) => {
try {
const matched = strPixels.match(PixelsRegex);
if (!matched)
return t.failure(u, c);
/* eslint-disable @typescript-eslint/no-non-null-assertion */
const parsed = parseInt(matched[1]);
return t.success(parsed);
}
catch {
return t.failure(u, c);
}
})), String);