printmaker
Version:
Generate PDF documents and from JavaScript objects
137 lines • 4.51 kB
JavaScript
/**
* Returns a copy of an object with all undefined values removed.
*
* @param obj The input object.
* @returns A copy of the input object with all undefined values removed.
*/
export function pickDefined(obj) {
const result = {};
for (const key in obj) {
if (typeof obj[key] !== 'undefined') {
result[key] = obj[key];
}
}
return result;
}
export function getFrom(object, name, fn) {
return check(object[name], name, fn);
}
export function check(value, name, fn) {
var _a, _b;
try {
return (_a = fn === null || fn === void 0 ? void 0 : fn(value)) !== null && _a !== void 0 ? _a : value;
}
catch (error) {
if (error.message === 'Missing value') {
throw new TypeError(`Missing value for "${name}"`);
}
if ((_b = error.message) === null || _b === void 0 ? void 0 : _b.startsWith('Invalid value for "')) {
const tail = error.message.replace(/^Invalid value for "/, '');
const glue = tail.startsWith('[') ? '' : '.';
throw new TypeError(`Invalid value for "${name}${glue}${tail}`);
}
throw new TypeError(`Invalid value for "${name}": ${error.message}`);
}
}
export function optional(fn) {
return (value) => {
if (value === undefined)
return undefined;
return fn ? fn(value) : value;
};
}
export function required(fn) {
return (value) => {
if (value === undefined)
throw new TypeError(`Missing value`);
return fn ? fn(value) : value;
};
}
export function asBoolean(value) {
if (typeof value === 'boolean')
return value;
throw typeError('boolean', value);
}
export function asString(value) {
if (typeof value === 'string')
return value;
throw typeError('string', value);
}
export function asNumber(value) {
if (Number.isFinite(value))
return value;
throw typeError('number', value);
}
export function asNonNegNumber(value) {
if (asNumber(value) >= 0)
return value;
throw typeError('non-negative number', value);
}
export function asDate(value) {
if (value instanceof Date)
return value;
throw typeError('Date', value);
}
export function asArray(value) {
if (Array.isArray(value))
return value;
throw typeError('array', value);
}
export function asObject(value) {
if (isObject(value))
return value;
throw typeError('object', value);
}
export function isObject(value) {
return (value != null &&
typeof value === 'object' &&
!Array.isArray(value) &&
value.toString() === '[object Object]');
}
export function typeError(expected, value) {
return new TypeError(`Expected ${expected}, got: ${printValue(value)}`);
}
export function printValue(value, refs) {
if (typeof value === 'string')
return `'${value}'`;
if (Array.isArray(value))
return printArray(value, refs);
if (value instanceof Date)
return `Date ${value.toISOString()}`;
if (value instanceof Function) {
return value.name ? `function ${value.name}` : 'anonymous function';
}
if (value instanceof ArrayBuffer)
return `ArrayBuffer ${printArray([...new Uint8Array(value)])}`;
if (ArrayBuffer.isView(value)) {
return `${value.constructor.name} ${printArray([...new Uint8Array(value.buffer)])}`;
}
const str = `${value}`;
if (str === '[object Object]')
return printObject(value, refs);
return str;
}
function printArray(array, refs) {
if (refs === null || refs === void 0 ? void 0 : refs.includes(array))
return 'recursive ref';
const maxElements = 8;
const content = array
.slice(0, maxElements)
.map((v) => printValue(v, [...(refs !== null && refs !== void 0 ? refs : []), array]))
.join(', ');
const tail = array.length > maxElements ? ', …' : '';
return `[${content}${tail}]`;
}
function printObject(object, refs) {
if (refs === null || refs === void 0 ? void 0 : refs.includes(object))
return 'recursive ref';
const maxEntries = 8;
const entries = Object.entries(object);
const tail = entries.length > maxEntries ? ', …' : '';
const main = entries
.slice(0, maxEntries)
.map(([key, value]) => `${key}: ${printValue(value, [...(refs !== null && refs !== void 0 ? refs : []), object])}`)
.join(', ');
return `{${main}${tail}}`;
}
//# sourceMappingURL=types.js.map