UNPKG

wkr-util

Version:
109 lines (67 loc) 3.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.trimStrDeep = exports.trimStr = exports.toUuid = exports.dateToStamp = exports.dateToStampMs = exports.nowStamp = exports.nowStampMs = exports.formatDatetime = exports.fromJson = exports.guardPlainObj = exports.formatIpv4 = exports.responseData = exports.responseError = exports.responseFromValidationResult = void 0; var _f = require("@cullylarson/f"); var _isUuid = _interopRequireDefault(require("is-uuid")); var _parseISO = _interopRequireDefault(require("date-fns/parseISO")); var _fromUnixTime = _interopRequireDefault(require("date-fns/fromUnixTime")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const isPlainObj = x => typeof x === 'object' && x.constructor === Object; const responseFromValidationResult = (0, _f.pick)(['errors', 'paramErrors']); exports.responseFromValidationResult = responseFromValidationResult; const responseError = messages => ({ errors: (0, _f.liftA)(messages) }); exports.responseError = responseError; const responseData = response => { return response.json().then(data => ({ response, data })).catch(_ => ({ response, data: {} })); // just assume the JSON was bad, return empty object }; // splits off any ipv6 stuff (e.g. node seems to like '::ffff:172.27.0.1' as an ip) and returns the ipv4 bit. if // the ipv4 bit doesn't match an IP, will return undefined exports.responseData = responseData; const formatIpv4 = x => x && (0, _f.isString)(x) ? (0, _f.compose)(x => /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/.test(x) ? x : undefined, _f.last, (0, _f.split)(':'))(x) : undefined; // makes sure the provided value is an object exports.formatIpv4 = formatIpv4; const guardPlainObj = (0, _f.curry)((defaultValue, x) => { return isPlainObj(x) ? x : defaultValue; }); // if x is an object, will just return it exports.guardPlainObj = guardPlainObj; const fromJson = (0, _f.curry)((defaultValue, x) => { // already an object, just return it as-is if ((0, _f.isObject)(x)) return x; try { return x && (0, _f.isString)(x) ? JSON.parse(x) : defaultValue; } catch (_) { return defaultValue; } }); // takes a date string (that date-fns/parseISO can understand), a unix timestamp, or a Date object and returns an ISO 8601 date (the same outputted by Date.prototype.toISOString. E.g. 2011-10-05T14:48:00.000Z) exports.fromJson = fromJson; const formatDatetime = x => { if (!x) return x; const dateObj = (0, _f.isString)(x) ? (0, _parseISO.default)(x) : (0, _f.isObject)(x) ? x : (0, _fromUnixTime.default)(x); return dateObj.toISOString(); }; // timestamp in ms exports.formatDatetime = formatDatetime; const nowStampMs = () => new Date().valueOf(); // timestamp in s exports.nowStampMs = nowStampMs; const nowStamp = () => Math.floor(nowStampMs() / 1000); // converts a Date or a string that date-fns can understand to a timestamp (milliseconds) exports.nowStamp = nowStamp; const dateToStampMs = x => (0, _f.isString)(x) ? (0, _parseISO.default)(x).valueOf() : x.valueOf(); // converts a Date or a string that date_fns can understand to a timestamp (seconds) exports.dateToStampMs = dateToStampMs; const dateToStamp = x => Math.floor(dateToStampMs(x) / 1000); // if x is a uuid, will return it unaltered. otherwise will // return an empty string. exports.dateToStamp = dateToStamp; const toUuid = x => x && _isUuid.default.v1(x) ? x : ''; // only trim if the value is a string, otherwise return the value unaltered exports.toUuid = toUuid; const trimStr = x => (0, _f.isString)(x) ? x.trim() : x; // trimStr except it will trim all children, children's children, etc. exports.trimStr = trimStr; const trimStrDeep = x => { return (0, _f.isObject)(x) || Array.isArray(x) ? (0, _f.map)(trimStrDeep, x) : trimStr(x); }; exports.trimStrDeep = trimStrDeep;