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
| // Definition for a binary tree node. | |
| function TreeNode(val, left, right) { | |
| this.val = val === undefined ? 0 : val; | |
| this.left = left === undefined ? null : left; | |
| this.right = right === undefined ? null : right; | |
| } | |
| /** | |
| * @param {Array<Number>} nodes | |
| * @return {null | TreeNode} |
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
| export default class Timer { | |
| constructor(time = 10, cb = () => console.log('null callback')) { | |
| this.time = time | |
| this.cb = cb | |
| this.remain = time * 1000 | |
| } | |
| start() { | |
| this.startTime = new Date() | |
| this.remain = this.time * 1000 |
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
| function futch(url, opts={}, onProgress) { | |
| return new Promise( (res, rej)=>{ | |
| var xhr = new XMLHttpRequest(); | |
| xhr.open(opts.method || 'get', url); | |
| for (var k in opts.headers||{}) | |
| xhr.setRequestHeader(k, opts.headers[k]); | |
| xhr.onload = e => res(e.target.responseText); | |
| xhr.onerror = rej; | |
| if (xhr.upload && onProgress) | |
| xhr.upload.onprogress = onProgress; // event.loaded / event.total * 100 ; //event.lengthComputable |
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
| 'An aplle is not a banana'.replace(/a/g, (() => { | |
| var number = 0; | |
| return () => { | |
| return number++; | |
| } | |
| })()) |
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
| navigator.sayswho= (function(){ | |
| var ua= navigator.userAgent, tem, | |
| M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; | |
| if(/trident/i.test(M[1])){ | |
| tem= /\brv[ :]+(\d+)/g.exec(ua) || []; | |
| return 'IE '+(tem[1] || ''); | |
| } | |
| if(M[1]=== 'Chrome'){ | |
| tem= ua.match(/\b(OPR|Edge)\/(\d+)/); | |
| if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera'); |
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
| Function.prototype.curry = function() { | |
| if (arguments.length < 1) { | |
| return this; | |
| } | |
| const self = this; | |
| const args = toArray(arguments); | |
| return function() { | |
| return self.apply(this, args.concat(toArray(arguments))); |
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
| var CustomObject = function () { | |
| var _this = this; | |
| _this.events = {}; | |
| _this.addEventListener = function(name, handler) { | |
| if (_this.events.hasOwnProperty(name)) | |
| _this.events[name].push(handler); | |
| else | |
| _this.events[name] = [handler]; | |
| }; |
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
| # Definition for singly-linked list. | |
| class ListNode(object): | |
| def __init__(self, x): | |
| self.val = x | |
| self.next = None | |
| class Solution(object): | |
| def addTwoNumbers(self, l1, l2): | |
| """ | |
| :type l1: ListNode |
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
| var req = require.context("../someDir", true, /^(.*\.(js$))[^.]*$/igm); | |
| req.keys().forEach(function(key){ | |
| req(key); | |
| }); |
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
| const cachedFetch = (url, options) => { | |
| let expiry = 5 * 60 // 5 min default | |
| if (typeof options === 'number') { | |
| expiry = options | |
| options = undefined | |
| } else if (typeof options === 'object') { | |
| // I hope you didn't set it to 0 seconds | |
| expiry = options.seconds || expiry | |
| } | |
| // Use the URL as the cache key to sessionStorage |
NewerOlder