Last active
October 11, 2019 13:24
-
-
Save sudojunior/6df440932cb4d36bc3ef9f4f3dc445c6 to your computer and use it in GitHub Desktop.
JavaScript ports of various python modules.
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
export class Deque { | |
constructor(iterable, maxLen = null) { | |
this.iterable = iterable; | |
this.maxLen = maxLen; | |
} | |
append(value) {} | |
appendLeft(value) {} | |
clear() {} | |
copy() {} | |
count(value) {} | |
extend(iterable) {} | |
extendLeft(iterable) {} | |
index(value, start, stop) {} | |
insert(i, x) {} | |
pop() {} | |
popLeft() {} | |
remove(value) {} | |
reverse() {} | |
rotate(n) {} | |
} |
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
/** | |
* @param {string} typeName | |
* @param {string|string[]} fieldNames | |
* @param {boolean} [rename=false] | |
* @param {*[]} [defaults=null] | |
*/ | |
export function namedTuple(typeName, fieldNames, rename = false, defaults = []) { | |
// Vailidation | |
// Construction | |
class NamedTuple { | |
constructor(...args) { | |
for(let i = 0; i < fieldNames.length; i++) { | |
this[fieldNames[i]] = args[i] || defaults[i] || null; | |
} | |
} | |
static get fields() { | |
return fieldNames; | |
} | |
replace(...args) { | |
if(typeof args[0] === 'object') { | |
let options = args[0]; | |
for(let key of Object.keys(options)) { | |
if(fieldNames.indexOf(key) > -1) { | |
this[key] = options[key] | |
} | |
} | |
} else { | |
for(let i = 0; i < fieldNames.length; i++) { | |
this[fieldNames[i]] = args[i] || defaults[i] || this[fieldNames[i]]; | |
} | |
} | |
return this; | |
} | |
toString() { | |
return `${this.constructor.name}(${fieldNames.map((field) => `${field}=${this[field]}`).join(', ')})`; | |
} | |
} | |
Object.defineProperty(NamedTuple, 'name', {value: typeName}); | |
return NamedTuple; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment