Skip to content

Instantly share code, notes, and snippets.

@zoubin
zoubin / permutation.js
Created June 7, 2017 06:59
How to exhaust all permutations of an array of elements
function * permutation (input) {
let n = input.length
if (n === 0) {
yield []
} else {
let needle = input.pop()
for (let p of permutation(input)) {
for (let i = 0; i < n; i++) {
let q = p.slice()
q.splice(i, 0, needle)
@zoubin
zoubin / hackModuleInHtmlForVirtualMachines.js
Created August 7, 2016 16:58
Fix `require` in script tags of pages for electron renderer process
/**
* convert `Shared Folders\projects\electron-quick-start`
* to `Z:\projects\electron-quick-start`
* See https://github.com/electron/electron/issues/6760
*
* Copy the source code to your page
* or add a script tag with src refering to this module
* **BEFORE** any module `require` takes place.
*
* Now you can `require` modules in HTML script tags as you do in node.
@zoubin
zoubin / isValidateDate.js
Created June 29, 2016 10:40
Check if a given date string specifies a valid date
// http://jsbin.com/lamakud/edit?html,output
function isValidateDate(datestr) {
var ms = Date.parse(datestr)
if (isNaN(ms)) return false
return ~~(datestr.split(/[^d]/).pop()) === new Date(ms).getDate()
}
module.exports = isValidateDate
@zoubin
zoubin / isLeap.js
Created June 29, 2016 10:27
Check if the given year is a leap year
function isLeap(year) {
var d = new Date(year + '')
d.setMonth(1)
d.setDate(29)
return d.getDate() === 29
}
module.exports = isLeap