Repository URL to install this package:
|
Version:
1.1.16 ▾
|
const isEven = require('../math/even')
const isOdd = require('../math/odd')
const zip = require('../util/zip')
// @TODO cannot import toFlatPairs here because circular :-/
// @TODO move to deps
// @TODO can use .findIndex too
const findIndexPlusOne = (needle, haystack) => haystack.indexOf(needle) + 1
const atIndexPlusOne = (needle, haystack) =>
haystack[findIndexPlusOne(needle, haystack)]
const findIndexMinusOne = (needle, haystack) => haystack.indexOf(needle) - 1
const atIndexMinusOne = (needle, haystack) =>
haystack[findIndexMinusOne(needle, haystack)]
/**
* @desc allows any key val
* @type {Array}
* @HACK @FIXME @TODO
* - BABEL HATES EXTENDING NATIVES
* - Compiled with ts | buble to enable it
* - you've been able to do this for years
*
* @TODO Set
* https://stackoverflow.com/questions/7069052/how-to-obtain-index-of-a-given-set-element-without-iteration
*/
// class FlatPairArray extends Array {
// typed(type) {
// this.type = type
// return this
// }
// has(key) {
// return this.indexOf(key) !== -1
// }
// // push(key, value) {
// // return this.set(key, value)
// // }
// set(key, value) {
// if (this.has(key)) return this
// super.push(key)
// super.push(value)
// return this
// }
// get(key) {
// return atIndexPlusOne(key, this)
// }
// getKey(value) {
// return atIndexMinusOne(value, this)
// }
// }
/* eslint no-proto: "OFF" */
/* eslint no-shadow: "OFF" */
/* eslint prefer-includes/prefer-includes: "OFF" */
var FlatPairArray = (function(Array) {
function FlatPairArray() {
Array.apply(this, arguments)
}
if (Array) FlatPairArray.__proto__ = Array
FlatPairArray.prototype = Object.create(Array && Array.prototype)
FlatPairArray.prototype.constructor = FlatPairArray
// @TODO not implemented
FlatPairArray.prototype.typed = function typed(type) {
this.type = type
return this
}
// boolean
FlatPairArray.prototype.has = function has(key) {
return this.indexOf(key) !== -1
}
// @chainable
FlatPairArray.prototype.set = function set(key, value) {
if (this.has(key)) {
return this
}
Array.prototype.push.call(this, key)
Array.prototype.push.call(this, value)
return this
}
// @alias getValue
FlatPairArray.prototype.get = function get(key) {
return atIndexPlusOne(key, this)
}
FlatPairArray.prototype.getKey = function getKey(value) {
return atIndexMinusOne(value, this)
}
// @alias add, set
FlatPairArray.prototype.add = FlatPairArray.prototype.set
/**
* @NOTE can make `final` if we want for better perf but better api allows writable
* @type {Function}
* @return {Array} pre-allocated for the length
*/
Object.defineProperty(FlatPairArray.prototype, 'allocate', {
configurable: true,
enumerable: false,
get() {
return new Array(this.length)
},
})
/**
* @since 5.0.0-beta.9
* @memberOf structured
* @memberOf FlatPairArray
* @NOTE 1/2 are keys, so cannot use array/preAllocate
* @TODO if doing anything but isEven/isOdd, should not preAllocate
*
* @param {Function} indexPredicate match current index (isEven, isOdd, etc)
* @return {Array<*>} keys
*
* @example
* const pairs = new FlatPairArray([ [1, '2'], [3, '4'] ])
* //=> [1, '2', 3, '4']
*
* pairs.half(isEven)
* //=> []
*/
FlatPairArray.prototype.half = function(indexPredicate) {
const preAllocated = new Array(this.length / 2)
let index = 0
this.forEach(value => {
if (indexPredicate(index)) preAllocated[index] = value
index += 1
})
return preAllocated
}
/**
* @since 5.0.0-beta.9
* @memberOf structured
* @memberOf FlatPairArray
*
* @TODO wonder if there is a `.grow`
* to pre-allocate growth
* on an instantiated array
* before adding?
*
* @param {Pairs} pairs isPairs
* @return {FlatPairArray} @chainable
*
* @example
*
* new FlatPairArray().addPairs([ [1, '2'], [3, '4'] ])
* //=> [1, '2', 3, '4']
*
*/
FlatPairArray.prototype.addPairs = function(pairs) {
for (let index = 0; index < pairs.length; index++) {
const [key, value] = pairs[index]
this.push(key)
this.push(value)
}
return this
}
// return Array<Pair>
FlatPairArray.prototype.toPairs = function() {
const keys = this.keys()
const values = this.values()
return zip(keys, values)
// const preAllocated = new Array(this.length / 2)
// let current = []
// let index = 0
// this.forEach(value => {
// if (isEven(index)) {
// current.push(value)
// }
// else {
// current.push(value)
// preAllocated[index / 2] = current.slice()
// current = []
// }
// index += 1
// })
// return preAllocated
}
// @return {Array}
FlatPairArray.prototype.keys = function() {
return this.half(isOdd)
}
// @override
// @return {Array}
FlatPairArray.prototype.values = function() {
return this.half(isEven)
}
return FlatPairArray
})(Array)
Object.assign(FlatPairArray, {
FlatPairArray,
FlatArrayPairs: FlatPairArray,
findIndexPlusOne,
atIndexPlusOne,
findIndexMinusOne,
atIndexMinusOne,
})
module.exports = FlatPairArray