@ribajs/utils
Advanced tools
Comparing version 1.9.0-alpha.0 to 1.9.0-alpha.1
{ | ||
"name": "@ribajs/utils", | ||
"description": "Utils module of Riba.js", | ||
"version": "1.9.0-alpha.0", | ||
"version": "1.9.0-alpha.1", | ||
"author": "Pascal Garber <pascal@artandcode.studio>", | ||
@@ -40,3 +40,9 @@ "contributors": [ | ||
"homepage": "https://github.com/ribajs/riba#readme", | ||
"directories": {} | ||
"directories": {}, | ||
"devDependencies": { | ||
"@babel/runtime": "^7.10.4", | ||
"@babel/runtime-corejs3": "^7.10.4", | ||
"@ribajs/tsconfig": "1.9.0-alpha.1", | ||
"babel-jest": "^26.1.0" | ||
} | ||
} |
@@ -12,2 +12,2 @@ /** | ||
return color; | ||
} | ||
}; |
@@ -1,2 +0,2 @@ | ||
import { Deferred } from './types' | ||
import { Deferred } from "./types"; | ||
@@ -7,3 +7,3 @@ export const times = (n: number, cb: () => void) => { | ||
} | ||
} | ||
}; | ||
@@ -24,3 +24,3 @@ /** | ||
return obj as Deferred; | ||
} | ||
}; | ||
@@ -27,0 +27,0 @@ /** |
133
src/dom.ts
@@ -0,1 +1,2 @@ | ||
export const MAX_UID = 1000; | ||
@@ -22,3 +23,3 @@ /** | ||
return results; | ||
} else if (el.getAttribute("contenteditable")) { | ||
} else if (el.hasAttribute("contenteditable")) { | ||
return el.innerHTML; // TODO write test for contenteditable | ||
@@ -28,5 +29,18 @@ } else { | ||
} | ||
} | ||
}; | ||
export const elementIsHidden = (el: HTMLElement) => { | ||
return ( | ||
el.hasAttribute("hidden") || | ||
el.style.display === "none" || | ||
el.style.visibility === "hidden" || | ||
window.getComputedStyle(el).display === "none" || | ||
window.getComputedStyle(el).visibility === "hidden" | ||
); | ||
}; | ||
export const elementIsVisable = (el: HTMLElement) => { | ||
return !elementIsHidden(el); | ||
}; | ||
/** | ||
@@ -82,3 +96,3 @@ * Scrolls to an element by event and selector | ||
}); | ||
} | ||
}; | ||
@@ -92,3 +106,3 @@ export const getElementFromEvent = (event: Event | MouseEvent | TouchEvent) => { | ||
return el; | ||
} | ||
}; | ||
@@ -108,5 +122,48 @@ export const getViewportDimensions = () => { | ||
}; | ||
} | ||
}; | ||
/** | ||
* Select all of an contenteditable or input element | ||
* @param element The element you want to select | ||
*/ | ||
export const selectAll = (element: HTMLInputElement) => { | ||
// need setTimeout for safari | ||
setTimeout(() => { | ||
if (typeof element.selectionStart !== "undefined") { | ||
element.selectionStart = 0; | ||
element.selectionEnd = 999; | ||
} | ||
if (typeof element.select === "function") { | ||
element.select(); | ||
} | ||
if (typeof element.setSelectionRange === "function") { | ||
element.setSelectionRange(0, 999); | ||
} | ||
if (window.getSelection) { | ||
const range = document.createRange(); | ||
range.selectNodeContents(element); | ||
const selection = window.getSelection(); | ||
if (selection) { | ||
selection.removeAllRanges(); | ||
selection.addRange(range); | ||
selection.selectAllChildren(element); | ||
} | ||
} | ||
if ((document as any).body.createTextRange) { | ||
const range: any = (document.body as any).createTextRange(); // Creates TextRange object | ||
range.moveToElementText(element); // sets Range | ||
range.select(); // make selection. | ||
} | ||
if (document.execCommand) { | ||
document.execCommand("selectAll", false, undefined); | ||
} | ||
}, 0); | ||
}; | ||
/** | ||
* Cross-browser Document Ready check | ||
@@ -138,2 +195,66 @@ * @see https://www.competa.com/blog/cross-browser-document-ready-with-vanilla-javascript/ | ||
checkReady(); | ||
} | ||
}; | ||
export const loadScript = async ( | ||
src: string, | ||
id: string, | ||
async = true, | ||
defer = true | ||
) => { | ||
const script = await new Promise<HTMLScriptElement>((resolve, reject) => { | ||
let script = document.getElementById(id) as HTMLScriptElement | null; | ||
if (script) { | ||
console.warn("script already loaded, do nothing."); | ||
if (script.hasAttribute("loaded")) { | ||
return resolve(script); | ||
} | ||
} else { | ||
script = document.createElement("script"); | ||
script.type = "text/javascript"; | ||
script.id = id; | ||
script.src = src; | ||
if (async) { | ||
script.async = true; | ||
} | ||
if (defer) { | ||
script.defer = true; | ||
} | ||
document.getElementsByTagName("head")[0].appendChild(script); | ||
} | ||
// IE | ||
if ((script as any).readyState) { | ||
(script as any).onreadystatechange = function () { | ||
if ( | ||
(script as any).readyState === "loaded" || | ||
(script as any).readyState === "complete" | ||
) { | ||
(script as any).onreadystatechange = null; | ||
resolve(script as HTMLScriptElement); | ||
} | ||
}; | ||
} | ||
// Other browsers | ||
script.onload = function () { | ||
resolve(script as HTMLScriptElement); | ||
}; | ||
script.onerror = function (...args) { | ||
const error = new Error("Error on load script " + script?.src); | ||
console.error(error); | ||
console.error(...args); | ||
reject(error); | ||
}; | ||
}); | ||
script.setAttribute("loaded", "true"); | ||
return script; | ||
}; | ||
export const getUID = (prefix: string): string => { | ||
do { | ||
prefix += ~~(Math.random() * MAX_UID); // "~~" acts like a faster Math.floor() here | ||
} while (document.getElementById(prefix)); | ||
return prefix; | ||
}; |
@@ -1,6 +0,6 @@ | ||
export * from './types'; | ||
export * from './color'; | ||
export * from './control'; | ||
export * from './dom'; | ||
export * from './type'; | ||
export * from './url'; | ||
export * from "./types"; | ||
export * from "./color"; | ||
export * from "./control"; | ||
export * from "./dom"; | ||
export * from "./type"; | ||
export * from "./url"; |
157
src/type.ts
@@ -1,2 +0,1 @@ | ||
export const couldBeJson = (str?: string | null) => { | ||
@@ -7,4 +6,4 @@ if (!str || typeof str !== "string") { | ||
str = str.trim(); | ||
return str.startsWith("{") || str.startsWith("["); | ||
} | ||
return str.charAt(0) === "{" || str.charAt(0) === "["; | ||
}; | ||
@@ -25,3 +24,3 @@ /** | ||
} | ||
} | ||
}; | ||
@@ -33,3 +32,3 @@ /** | ||
return typeof value === "undefined"; | ||
} | ||
}; | ||
@@ -41,3 +40,3 @@ /** | ||
return !isUndefined(value); | ||
} | ||
}; | ||
@@ -48,5 +47,5 @@ /** | ||
*/ | ||
export const isObject = (obj: object) => { | ||
export const isObject = (obj: any) => { | ||
return isDefined(obj) && typeof obj === "object" && obj !== null; | ||
} | ||
}; | ||
@@ -57,5 +56,5 @@ /** | ||
*/ | ||
export const getString = (value: string) => { | ||
return value != null ? value.toString() : undefined; | ||
} | ||
export const getString = (value: any) => { | ||
return value?.toString ? value.toString() : undefined; | ||
}; | ||
@@ -68,3 +67,3 @@ /** | ||
return value ? parseFloat(value) : undefined; | ||
} | ||
}; | ||
@@ -90,3 +89,3 @@ /** | ||
return object; | ||
} | ||
}; | ||
@@ -98,3 +97,3 @@ /** | ||
return typeof value === "function"; | ||
} | ||
}; | ||
@@ -107,3 +106,3 @@ /** | ||
return Object.prototype.toString.call(value) === "[object Array]"; | ||
} | ||
}; | ||
@@ -116,3 +115,3 @@ /** | ||
return !isNaN(parseFloat(value)) && !isNaN(value - 0); | ||
} | ||
}; | ||
@@ -125,3 +124,3 @@ /** | ||
return typeof value === typeof true; | ||
} | ||
}; | ||
@@ -133,3 +132,3 @@ /** | ||
return isDefined(value) && typeof value === "string"; | ||
} | ||
}; | ||
@@ -141,3 +140,3 @@ /** | ||
return isString(value) && /\d/.test(value); | ||
} | ||
}; | ||
@@ -149,3 +148,3 @@ /** | ||
return /^\d+$/.test(value); | ||
} | ||
}; | ||
@@ -157,3 +156,3 @@ /** | ||
return /^[0-9 ()+-]+$/.test(value); | ||
} | ||
}; | ||
@@ -172,3 +171,3 @@ /** | ||
} | ||
} | ||
}; | ||
@@ -185,3 +184,3 @@ export const escapeHtml = (str: string) => { | ||
}); | ||
} | ||
}; | ||
@@ -205,63 +204,67 @@ export const stripHtml = (html: string) => { | ||
/** | ||
* Merge the contents of two or more objects together into the first object. | ||
* @param {boolean} deep If true, the merge becomes recursive (aka. deep copy). | ||
* @param {object} target An object that will receive the new properties | ||
* @param {any[]} objects The objects containing additional properties to merge in. | ||
* @see http://www.damirscorner.com/blog/posts/20180216-VariableNumberOfArgumentsInTypescript.html | ||
*/ | ||
export const extend = (deep: boolean, extended: any = {}, ...objects: any[]) => { | ||
// Merge the object into the extended object | ||
const merge = (obj: any) => { | ||
for (const prop in obj) { | ||
if (obj[prop]) { | ||
if ( | ||
deep && | ||
Object.prototype.toString.call(obj[prop]) === "[object Object]" | ||
) { | ||
// If we're doing a deep merge and the property is an object | ||
extended[prop] = extend(true, extended[prop], obj[prop]); | ||
} else { | ||
// Otherwise, do a regular merge | ||
extended[prop] = obj[prop]; | ||
} | ||
/** | ||
* Merge the contents of two or more objects together into the first object. | ||
* @param deep If true, the merge becomes recursive (aka. deep copy). | ||
* @param target An object that will receive the new properties | ||
* @param objects The objects containing additional properties to merge in. | ||
* @see http://www.damirscorner.com/blog/posts/20180216-VariableNumberOfArgumentsInTypescript.html | ||
*/ | ||
export const extend = ( | ||
deep: boolean, | ||
extended: any = {}, | ||
...objects: any[] | ||
) => { | ||
// Merge the object into the extended object | ||
const merge = (obj: any) => { | ||
for (const prop in obj) { | ||
if (obj[prop]) { | ||
if ( | ||
deep && | ||
Object.prototype.toString.call(obj[prop]) === "[object Object]" | ||
) { | ||
// If we're doing a deep merge and the property is an object | ||
extended[prop] = extend(true, extended[prop], obj[prop]); | ||
} else { | ||
// Otherwise, do a regular merge | ||
extended[prop] = obj[prop]; | ||
} | ||
} | ||
}; | ||
// Loop through each object and conduct a merge | ||
for (let i = 0; i < objects.length; i++) { | ||
merge(objects[i]); | ||
} | ||
}; | ||
return extended; | ||
// Loop through each object and conduct a merge | ||
for (let i = 0; i < objects.length; i++) { | ||
merge(objects[i]); | ||
} | ||
/** | ||
* Concat the contents of two objects together into the first object and return the concatenated object. | ||
* @param {boolean} deep If true, the merge becomes recursive (aka. deep copy). | ||
* @param {object} object1 An first object containing properties to concat. | ||
* @param {object} object2 The second object containing properties to concat. | ||
*/ | ||
export const concat = (deep: boolean, object1?: object, object2?: object): any => { | ||
object1 = extend(deep, object1 || {}, object1 || {}, object2 || {}); | ||
return object1; | ||
} | ||
return extended; | ||
}; | ||
/** | ||
* Clone an object or array | ||
* @param deep If true, the merge becomes recursive (aka. deep copy). | ||
* @param val The value(s) to clone | ||
*/ | ||
export const clone = (deep: boolean, val: any) => { | ||
if (isArray(val)) { | ||
return val.slice(); | ||
} | ||
if (isObject(val)) { | ||
return extend(deep, {}, val); | ||
} | ||
if (isString(val)) { | ||
return val.repeat(1); | ||
} | ||
return val; | ||
/** | ||
* Concat the contents of two objects together into the first object and return the concatenated object. | ||
* @param deep If true, the merge becomes recursive (aka. deep copy). | ||
* @param object1 An first object containing properties to concat. | ||
* @param object2 The second object containing properties to concat. | ||
*/ | ||
export const concat = (deep: boolean, object1?: any, object2?: any): any => { | ||
object1 = extend(deep, object1 || {}, object1 || {}, object2 || {}); | ||
return object1; | ||
}; | ||
/** | ||
* Clone an object or array | ||
* @param deep If true, the merge becomes recursive (aka. deep copy). | ||
* @param val The value(s) to clone | ||
*/ | ||
export const clone = (deep: boolean, val: any) => { | ||
if (isArray(val)) { | ||
return val.slice(); | ||
} | ||
if (isObject(val)) { | ||
return extend(deep, {}, val); | ||
} | ||
if (isString(val)) { | ||
return val.repeat(1); | ||
} | ||
return val; | ||
}; |
@@ -1,1 +0,1 @@ | ||
export * from './deferred'; | ||
export * from "./deferred"; |
@@ -18,3 +18,3 @@ /** | ||
return decodeURIComponent(results[2].replace(/\+/g, " ")); | ||
} | ||
}; | ||
@@ -44,3 +44,3 @@ /** | ||
return (l as any) as Location; | ||
} | ||
}; | ||
@@ -61,3 +61,3 @@ /** | ||
} | ||
} | ||
}; | ||
@@ -78,3 +78,3 @@ /** | ||
); | ||
} | ||
}; | ||
@@ -121,3 +121,3 @@ /** | ||
return url.replace(/#.*/, ""); | ||
} | ||
}; | ||
@@ -145,3 +145,3 @@ /** | ||
} | ||
} | ||
}; | ||
@@ -163,3 +163,3 @@ /** | ||
return isAbsolute; | ||
} | ||
}; | ||
@@ -183,4 +183,2 @@ export const isExternalUrl = (absoluteUrl: string) => { | ||
/** | ||
@@ -191,3 +189,3 @@ * Get hash from address bar or url if set | ||
return getLocation(url).hash; | ||
} | ||
}; | ||
@@ -199,3 +197,3 @@ /** | ||
return (window.location.hash = hash); | ||
} | ||
}; | ||
@@ -211,2 +209,2 @@ /** | ||
); | ||
} | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21046
714
4