Last active
March 6, 2019 15:53
-
-
Save justinrlle/5d340450cd787125824f to your computer and use it in GitHub Desktop.
Some utils I came and will come to code in js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const range = (start, end, step = 1) => | |
Array.from( | |
new Array(Math.ceil((end - start) / step)), | |
(x, i) => i * step + start | |
); | |
export { range }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const calcTextWidth = (style, string) => { | |
// be sure to be string | |
const str = String(string); | |
const defaultStyle = 'position: absolute; visibility: hidden; height: auto; width: auto; ' | |
+ ' white-space: nowrap'; | |
const tmpEl = document.createElement('div'); | |
let customStyle = ''; | |
let prop = ''; | |
let width = 0; | |
for (prop in style) { | |
if (style.hasOwnProperty(prop)) { | |
customStyle += prop + ': ' + style[prop] + '; '; | |
} | |
} | |
if (tmpEl.textContent) { | |
tmpEl.textContent = str; | |
} else { | |
tmpEl.innerText = str; | |
} | |
tmpEl.style.cssText = customStyle + defaultStyle; | |
document.body.appendChild(tmpEl); | |
width = tmpEl.getBoundingClientRect().width; | |
tmpEl.remove(); | |
return width; | |
}; | |
export { calcTextWidth }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const options = { | |
returnMode: 'function', | |
implType: 'function', | |
}; | |
const returnMode = (mode, prototypeChain) => { | |
switch (mode) { | |
case 'name': { | |
return prototypeChain.map(c => c.name || null); | |
} | |
case 'function': { | |
return prototypeChain; | |
} | |
default: { | |
let nextMode = ''; | |
if (mode === options.returnMode) nextMode = 'function'; | |
else nextMode = options.returnMode; | |
return returnMode(nextMode, prototypeChain); | |
} | |
} | |
}; | |
const getPrototypeChainOf = (obj, mode) => { | |
const cs = []; | |
let pt = obj; | |
do { | |
pt = Object.getPrototypeOf(pt); | |
if (pt) cs.push(pt.constructor || null); | |
} while (pt !== null); | |
return returnMode(mode, cs); | |
}; | |
const findFromWichPrototype = (obj, prop, mode) => { | |
const pc = getPrototypeChainOf(obj, 'function'); | |
let finalPt = undefined; | |
let propToStr; | |
if (prop.toString() === prop) { | |
propToStr = prop; | |
} else if (prop && typeof prop === 'function') { | |
propToStr = prop.name; | |
} else { | |
propToStr = String(prop); | |
} | |
for (const pt of pc) { | |
if (pt.prototype[propToStr]) { | |
switch (mode) { | |
case 'function': { | |
finalPt = pt; | |
break; | |
} | |
case 'string': { | |
finalPt = pt.name; | |
break; | |
} | |
default: { | |
finalPt = pt; | |
break; | |
} | |
} | |
break; | |
} | |
} | |
return finalPt; | |
}; | |
export { getPrototypeChainOf, findFromWichPrototype }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment