rfc6570-expand
Version:
A template processor for RFC 6570 URI Template
66 lines (65 loc) • 2.05 kB
JavaScript
;
const isArray = (variable) => {
return Array.isArray(variable) && !Array.isArray(variable[0]);
};
exports.isArray = isArray;
const isDefined = (variable) => {
return typeof variable !== 'undefined';
};
exports.isDefined = isDefined;
const isObject = (variable) => {
return Array.isArray(variable) && Array.isArray(variable[0]);
};
exports.isObject = isObject;
const isString = (variable) => {
return typeof variable === 'string';
};
exports.isString = isString;
const normalizeVariable = (variable) => {
if (typeof variable === 'undefined')
return undefined;
if (variable === null)
return undefined;
if (typeof variable === 'boolean')
return String(variable);
if (typeof variable === 'number')
return String(variable);
if (typeof variable === 'string')
return variable;
if (typeof variable === 'object') {
if (Array.isArray(variable)) {
return variable.map(toString);
}
else {
return Object.keys(variable).map((key) => {
return [key, toString(variable[key])];
});
}
}
if (typeof variable === 'function')
return variable.toString();
return variable.toString();
};
const normalizeVariables = (variables) => {
return Object.keys(variables).map(key => {
return [key, normalizeVariable(variables[key])];
}).reduce((normalized, [key, value]) => {
return Object.assign(normalized, { [key]: value });
}, {});
};
exports.normalizeVariables = normalizeVariables;
const toString = (variable) => {
if (typeof variable === 'undefined')
return undefined;
if (variable === null)
return undefined;
if (typeof variable === 'boolean')
return String(variable);
if (typeof variable === 'number')
return String(variable);
if (typeof variable === 'string')
return variable;
if (typeof variable === 'function')
return variable.toString();
return variable.toString();
};