Skip to content

Instantly share code, notes, and snippets.

View jovey-zheng's full-sized avatar
🎯
Focusing

Jovey Zheng jovey-zheng

🎯
Focusing
View GitHub Profile
@jovey-zheng
jovey-zheng / getElementTop.js
Last active August 2, 2017 06:52
Get element top in document
function getElementTop(elem) {
  var elemTop = elem.offsetTop;
  elem = elem.offsetParent;
  while (elem != null) {
    elemTop += elem.offsetTop;
    elem = elem.offsetParent;
  }
  return elemTop;
@jovey-zheng
jovey-zheng / syncApply.js
Created September 16, 2017 07:12
Sync apply function
syncApply ({timeout, fn, args = [], scope = this}) {
return new Promise((resolve, reject) => {
try {
setTimeout(() => {
resolve(fn.apply(scope, args))
}, timeout)
} catch (err) {
reject(err)
}
})
@jovey-zheng
jovey-zheng / flatten.js
Last active May 6, 2019 07:15
Flatten deep nested arrays.
// array flatten function
function flatten (arr) {
let result = [].slice.call(arguments)[1] || []
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
flatten(arr[i], result)
} else {
result.push(arr[i])
}
@jovey-zheng
jovey-zheng / sumBigNumber.js
Created May 5, 2019 03:03
Calculate the sum of large numbers.
/**
* @param a {String}
* @param b {String}
*/
function sumBigNumber(a, b) {
if (typeof a !== 'string' || typeof b !== 'string') {
throw new Error('param 'a' and 'b' must be string')
}
var res = '',