View nodash.js
This file contains 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
// follow python range func. | |
function range(start, end = null, step = 1) { | |
if (end === null) return Array(start).fill().map((_, index) => index) | |
const result = [] | |
for (let i = start; step > 0 ? i < end : i > end; i += step || 1) { | |
result.push(i) | |
} | |
return result | |
} |
View composeReactElements.ts
This file contains 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
import React from 'react'; | |
const genContainer = | |
(name: string): React.FC => | |
({ children }) => | |
( | |
<div> | |
<h1>I'm {name}</h1> | |
<div style={{ paddingLeft: '10px' }}>{children}</div> | |
</div> |
View queue.ts
This file contains 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
class Node<T> { | |
value: T | |
next?: Node<T> | |
constructor(value: T) { | |
this.value = value | |
} | |
} | |
class Queue<T> { | |
private _size = 0 |
View solution.js
This file contains 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
let queue = []; | |
let running = false; | |
function start(id) { | |
queue.push(id); | |
if (running) return; | |
next(); | |
} | |
function next() { | |
running = true; |
View shallowClone.js
This file contains 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
function shallowClone(obj) { | |
if (typeof obj !== 'object') return obj; | |
const newObj = obj instanceof Array ? [] : {}; | |
for (let key of Object.keys(obj)) { | |
newObj[key] = obj[key]; | |
} | |
return newObj; | |
} |
View deepClone.js
This file contains 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
function deepCopySimple(obj) { | |
return JSON.parse(JSON.stringify(obj)); | |
} | |
function deepClone(obj) { | |
if (typeof obj !== 'object') return obj; | |
const newObj = obj instanceof Array ? [] : {}; | |
for (let key of Object.keys(obj)) { | |
if (typeof obj[key] !== 'object') { |
View eventBus.js
This file contains 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
class EventBus { | |
constructor() { | |
this.eventMap = {} | |
this.onceEventMap = {} | |
} | |
on(eventName, listener) { | |
if (!this.eventMap[eventName]) { | |
this.eventMap[eventName] = [listener] | |
} else { |
View storage.ts
This file contains 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
abstract class CustomStorage { | |
abstract storage: any; | |
abstract getItem<T>(key: string): T | null; | |
abstract setItem(key: string, value: any): void; | |
abstract removeItem(key: string): void; | |
abstract clear(): void; | |
getItemAndDelete<T>(key: string): T | null { |
View curry.js
This file contains 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
function curry(fn) { | |
return function curried(...args) { | |
if (fn.length === args.length) { | |
return fn.apply(this, args); | |
} | |
return (...extraArgs) => { | |
return curried.apply(this, args.concat(extraArgs)); | |
}; | |
}; | |
} |
View debounce.js
This file contains 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
function debounce(fn, delay) { | |
let timerId = null; | |
return (...args) => { | |
clearTimeout(timerId); | |
timerId = setTimeout(() => { | |
fn(args) | |
}, delay) | |
} | |
} |
NewerOlder