@therms/web-js
Advanced tools
Comparing version 1.6.1 to 1.7.0
@@ -0,1 +1,8 @@ | ||
# [1.7.0](https://bitbucket.org/thermsio/web-js/compare/v1.6.1...v1.7.0) (2022-03-01) | ||
### Features | ||
* add utils ([3b87989](https://bitbucket.org/thermsio/web-js/commits/3b8798988a0ccad0afce1d27a41edcb3fd2f67f6)) | ||
## [1.6.1](http://bitbucket.org/thermsio/web-js/compare/v1.6.0...v1.6.1) (2022-01-28) | ||
@@ -2,0 +9,0 @@ |
160
dist/cjs.js
@@ -222,2 +222,10 @@ 'use strict'; | ||
const deleteCookie = (name) => { | ||
Cookie__default["default"].remove(name); | ||
}; | ||
const getCookie = (name) => Cookie__default["default"].get(name); | ||
const setCookie = (name, value) => { | ||
Cookie__default["default"].set(name, value); | ||
}; | ||
/** | ||
@@ -304,10 +312,15 @@ * This helper will format a JSON-schema "like" address (https://schema.org/PostalAddress). THERMS addresses are | ||
} | ||
function randomHEXColor() { | ||
// tslint:disable-next-line:no-bitwise | ||
return '#' + ((Math.random() * 0xffffff) << 0).toString(16).padStart(6, '0'); | ||
} | ||
const deleteCookie = (name) => { | ||
Cookie__default["default"].remove(name); | ||
const dateWithinRange = (from, to, date) => { | ||
const d = date || new Date().toISOString(); | ||
if (d < from) | ||
return false; | ||
if (d > to) | ||
return false; | ||
return true; | ||
}; | ||
const getCookie = (name) => Cookie__default["default"].get(name); | ||
const setCookie = (name, value) => { | ||
Cookie__default["default"].set(name, value); | ||
}; | ||
@@ -391,2 +404,62 @@ const MONTH_MIN = (365 / 12) * 24 * 60; | ||
function deepClone(obj) { | ||
if (!obj) | ||
return obj; | ||
if (Array.isArray(obj) || typeof obj === 'object') { | ||
try { | ||
return JSON.parse(JSON.stringify(obj)); | ||
} | ||
catch (e) { | ||
throw new Error(`unable to deepClone, failed to JSON.parse<-stringify, (${e.message})`); | ||
} | ||
} | ||
else { | ||
throw new Error('an array or object is required'); | ||
} | ||
} | ||
/** | ||
* Recursively freeze an object. | ||
* | ||
* @param object Target object to freeze | ||
* @param clone When true, the object itself and all nested objects are cloned so that the original object is not frozen | ||
*/ | ||
function deepFreeze(object, clone = true) { | ||
// Retrieve the property names defined on object | ||
const propNames = Object.getOwnPropertyNames(object); | ||
// Freeze properties before freezing self | ||
for (const name of propNames) { | ||
const value = object[name]; | ||
if (value && typeof value === 'object') { | ||
deepFreeze(value, clone); | ||
} | ||
} | ||
return Object.freeze(clone ? { ...object } : object); | ||
} | ||
function isObject(item) { | ||
return item && typeof item === 'object' && !Array.isArray(item); | ||
} | ||
function deepMerge(target, ...sources) { | ||
if (!sources.length) | ||
return target; | ||
const source = sources.shift(); | ||
if (isObject(target) && isObject(source)) { | ||
for (const key in source) { | ||
if (isObject(source[key])) { | ||
// @ts-ignore | ||
if (!target[key]) { | ||
Object.assign(target, { [key]: {} }); | ||
} | ||
// @ts-ignore | ||
deepMerge(target[key], source[key]); | ||
} | ||
else { | ||
Object.assign(target, { [key]: source[key] }); | ||
} | ||
} | ||
} | ||
return deepMerge(target, ...sources); | ||
} | ||
const permissionsState = { | ||
@@ -437,2 +510,57 @@ backgroundSync: 'prompt', | ||
function isEmail(email) { | ||
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
return re.test(String(email).toLowerCase()); | ||
} | ||
function isHexColor(str) { | ||
return /^#([0-9A-F]{3}){1,2}$/i.test(str); | ||
} | ||
const isIsoDate = (dateString) => { | ||
if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(dateString || '')) | ||
return false; | ||
return new Date(dateString).toISOString() === dateString; | ||
}; | ||
function isUrl(str) { | ||
const pattern = new RegExp('^(https?:\\/\\/)?' + // protocol | ||
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name | ||
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address | ||
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path | ||
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string | ||
'(\\#[-a-z\\d_]*)?$', 'i'); | ||
return !!pattern.test(str); | ||
} | ||
// @deprecated - will remove in next major release in favor of pluralize() | ||
function isPlural(array = [], text = '', postfix = 's') { | ||
if (Array.isArray(array) && array.length > 1) | ||
return text + postfix; | ||
if (typeof array === 'number' && array > 1) | ||
return text + postfix; | ||
if (typeof array === 'boolean' && array) | ||
return text + postfix; | ||
return text; | ||
} | ||
/** | ||
* The default pluralization will add an "s" to the end. If you need to pass a custom pluralized text, use an array | ||
* as the first arg, see example: | ||
* | ||
* @example pluralize('Location', 3) // "Locations" | ||
* @example pluralize(['Entry', 'Entries], 2) // "Entries" | ||
*/ | ||
function pluralize(txt, count = 0) { | ||
if (count > 1) { | ||
if (Array.isArray(txt)) { | ||
return txt[1]; | ||
} | ||
return txt + 's'; | ||
} | ||
if (Array.isArray(txt)) { | ||
return txt[0]; | ||
} | ||
return txt; | ||
} | ||
const DEFAULT_TIMEOUT = 30000; | ||
@@ -509,12 +637,2 @@ class PromiseTimeoutError extends Error { | ||
function isPlural(array = [], text = '', postfix = 's') { | ||
if (Array.isArray(array) && array.length > 1) | ||
return text + postfix; | ||
if (typeof array === 'number' && array > 1) | ||
return text + postfix; | ||
if (typeof array === 'boolean' && array) | ||
return text + postfix; | ||
return text; | ||
} | ||
exports.PromiseController = PromiseController; | ||
@@ -525,2 +643,6 @@ exports.PromiseTimeoutError = PromiseTimeoutError; | ||
exports.checkIsBrowserVisible = checkIsBrowserVisible; | ||
exports.dateWithinRange = dateWithinRange; | ||
exports.deepClone = deepClone; | ||
exports.deepFreeze = deepFreeze; | ||
exports.deepMerge = deepMerge; | ||
exports.deleteCookie = deleteCookie; | ||
@@ -536,5 +658,11 @@ exports.flashBrowserTitle = flashBrowserTitle; | ||
exports.humanizeMinutes = humanizeMinutes; | ||
exports.isEmail = isEmail; | ||
exports.isHexColor = isHexColor; | ||
exports.isIsoDate = isIsoDate; | ||
exports.isMobileDevice = isMobileDevice; | ||
exports.isPlural = isPlural; | ||
exports.isUrl = isUrl; | ||
exports.onBrowserFocusChange = onBrowserFocusChange; | ||
exports.pluralize = pluralize; | ||
exports.randomHEXColor = randomHEXColor; | ||
exports.retryPromise = retryPromise; | ||
@@ -541,0 +669,0 @@ exports.roundToNearestMin = roundToNearestMin; |
export * from './services/browser-focus'; | ||
export * from './services/browser-title'; | ||
export * from './services/browser-to-browser-communications'; | ||
export * from './services/cookies'; | ||
export * from './utils/address'; | ||
export * from './utils/color'; | ||
export * from './services/cookies'; | ||
export * from './utils/date-within-range'; | ||
export * from './utils/date'; | ||
export * from './utils/deep-clone'; | ||
export * from './utils/deep-freeze'; | ||
export * from './utils/deep-merge'; | ||
export * from './utils/device'; | ||
export * from './utils/is-email'; | ||
export * from './utils/is-hex-color'; | ||
export * from './utils/is-iso-date'; | ||
export * from './utils/is-url'; | ||
export * from './utils/pluralize'; | ||
export * from './utils/promises/promise-controller'; | ||
export * from './utils/promises/retry-promise'; | ||
export * from './utils/promises/sleep'; | ||
export * from './utils/strings'; | ||
export * from './services/vent'; |
export { checkIsBrowserVisible, onBrowserFocusChange } from './services/browser-focus.js'; | ||
export { flashBrowserTitle, setBrowserTitle } from './services/browser-title.js'; | ||
export { b2bComms } from './services/browser-to-browser-communications.js'; | ||
export { deleteCookie, getCookie, setCookie } from './services/cookies.js'; | ||
export { formatAddress } from './utils/address.js'; | ||
export { getColorContrast } from './utils/color.js'; | ||
export { deleteCookie, getCookie, setCookie } from './services/cookies.js'; | ||
export { getColorContrast, randomHEXColor } from './utils/color.js'; | ||
export { dateWithinRange } from './utils/date-within-range.js'; | ||
export { getNumberWithOrdinal, humanizeMillis, humanizeMinutes, roundToNearestMin, setLocaleFirstDayOfWeek } from './utils/date.js'; | ||
export { deepClone } from './utils/deep-clone.js'; | ||
export { deepFreeze } from './utils/deep-freeze.js'; | ||
export { deepMerge } from './utils/deep-merge.js'; | ||
export { getDevice, isMobileDevice } from './utils/device.js'; | ||
export { isEmail } from './utils/is-email.js'; | ||
export { isHexColor } from './utils/is-hex-color.js'; | ||
export { isIsoDate } from './utils/is-iso-date.js'; | ||
export { isUrl } from './utils/is-url.js'; | ||
export { isPlural, pluralize } from './utils/pluralize.js'; | ||
export { PromiseController, PromiseTimeoutError } from './utils/promises/promise-controller.js'; | ||
export { retryPromise } from './utils/promises/retry-promise.js'; | ||
export { sleep } from './utils/promises/sleep.js'; | ||
export { isPlural } from './utils/strings.js'; | ||
export { Vent, globalVent } from './services/vent.js'; | ||
//# sourceMappingURL=index.js.map |
@@ -15,1 +15,2 @@ /** | ||
}): string; | ||
export declare function randomHEXColor(): string; |
@@ -33,4 +33,8 @@ const calculatedMap = {}; | ||
} | ||
function randomHEXColor() { | ||
// tslint:disable-next-line:no-bitwise | ||
return '#' + ((Math.random() * 0xffffff) << 0).toString(16).padStart(6, '0'); | ||
} | ||
export { getColorContrast }; | ||
export { getColorContrast, randomHEXColor }; | ||
//# sourceMappingURL=color.js.map |
{ | ||
"name": "@therms/web-js", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "Common web/JS tools & utilities", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
91709
71
1462