Skip to content

Instantly share code, notes, and snippets.

Jovey Zheng jovey-zheng

Block or report user

Report or block jovey-zheng

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jovey-zheng
jovey-zheng / sumBigNumber.js
Created May 5, 2019
Calculate the sum of large numbers.
View sumBigNumber.js
/**
* @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 = '',
@jovey-zheng
jovey-zheng / flatten.js
Last active May 6, 2019
Flatten deep nested arrays.
View flatten.js
// 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 / syncApply.js
Created Sep 16, 2017
Sync apply function
View syncApply.js
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 / getElementTop.js
Last active Aug 2, 2017
Get element top in document
View getElementTop.js
function getElementTop(elem) {
  var elemTop = elem.offsetTop;
  elem = elem.offsetParent;
  while (elem != null) {
    elemTop += elem.offsetTop;
    elem = elem.offsetParent;
  }
  return elemTop;
View cache.js
// 简单的缓存工具
// 匿名函数创造了一个闭包
const cache = (function() {
const store = {};
return {
get(key) {
return store[key];
},
set(key, val) {
View isRepeat.js
function isRepeat(arr){
var hash = {};
for (var item of arr) {
if (hash[item]) {
return true;
}
hash[item] = false;
}
@jovey-zheng
jovey-zheng / numtoCl.js
Created Aug 3, 2016
Transform Number to Chinese.
View numtoCl.js
(function(global){
var numtoCL = {}
var Maxnumb = 9007199254740992;
numtoCL.toS = function(num,m){
var op = {
ch: '零一二三四五六七八九'
,ch_u: '个十百千万亿'
,other: '负点'
,toCL: this.toCL
View DateFormat.js
class DateFormat {
constructor(tmp) {
this.tmp = +tmp || Date.now();
}
duration(format = 'HH:mm:ss') {
let hour = ~~(this.tmp/1000/60/60);
let minutes = ~~((this.tmp/1000/60)%60);
let seconds = ~~((this.tmp/1000)%60);
@jovey-zheng
jovey-zheng / _.chunk.js
Created Jul 28, 2016
Transform an array to some chunk array.
View _.chunk.js
function chunk(arr, chunk) {
var res = [],
len = arr.length,
_len = Math.ceil(arr.length / chunk),
_chunk = 0;
chunk = chunk > len ? len : chunk;
for (var i = 0; i < _len ;i++) {
var _arr = [];
@jovey-zheng
jovey-zheng / merge.js
Created May 20, 2016
merge array based on lodash.
View merge.js
var _ = require('lodash');
module.exports = function (source, target) {
var joinArrays = function (a, b) {
if (_.isArray(a) && _.isArray(b)) {
return a.concat(b);
};
if (_.isPlainObject(a) && _.isPlainObject(b)) {
return _.merge(a, b, joinArrays);
};
You can’t perform that action at this time.