Commit c17715a2 authored by Phil Booth's avatar Phil Booth

feat(code): implement basic circular array type

parent 0ee6b3ab
'use strict'
class Hoopy extends Array {
constructor (size) {
let index, isIndexOverflowed
if (! isPositiveInteger(size)) {
throw new TypeError('Argument `size` must be a positive integer.')
}
super(size)
this.grow = by => {
if (! isPositiveInteger(by)) {
throw new TypeError('Argument `by` must be a positive integer.')
}
let i
const newSize = size + by
for (i = size; i < newSize; ++i) {
this[i] = undefined
}
if (isIndexOverflowed) {
for (i = 0; i <= index; ++i) {
this[size + i] = this[i]
this[i] = undefined
}
}
size = newSize
}
return new Proxy(this, {
get (target, key) {
if (isInteger(key)) {
return target[getIndex(key, size)]
}
return target[key]
},
set (target, key, value) {
if (isInteger(key)) {
index = getIndex(key, size)
target[index] = value
if (Math.abs(key) >= size) {
isIndexOverflowed = true
} else {
isIndexOverflowed = false
}
} else {
target[key] = value
}
return true
}
})
}
}
function isPositiveInteger (thing) {
return isInteger(thing) && thing > 0
}
function isInteger (thing) {
return +thing % 1 === 0
}
function getIndex (key, size) {
if (key === 0) {
return 0
}
if (key < 0) {
return (size - Math.abs(key)) % size
}
return key % size
}
Hoopy.prototype.push = () => {
throw new Error('Not implemented')
}
Hoopy.prototype.pop = () => {
throw new Error('Not implemented')
}
module.exports = Hoopy
/* eslint-env mocha */
/* eslint-disable max-nested-callbacks */
'use strict'
const { assert } = require('chai')
const Hoopy = require('.')
test('interface is correct', () => {
assert.isFunction(Hoopy)
assert.lengthOf(Hoopy, 1)
assert.throws(() => new Hoopy(0))
assert.doesNotThrow(() => new Hoopy(1))
assert.throws(() => new Hoopy(-1))
})
suite('instantiate, size=1:', () => {
let hoopy
setup(() => {
hoopy = new Hoopy(1)
})
test('instance is array', () => {
assert.isTrue(Array.isArray(hoopy))
})
test('length is correct', () => {
assert.equal(hoopy.length, 1)
})
test('[0] is undefined', () => {
assert.isUndefined(hoopy[0])
})
test('[1] is undefined', () => {
assert.isUndefined(hoopy[1])
})
test('[-1] is undefined', () => {
assert.isUndefined(hoopy[-1])
})
test('grow method is implemented', () => {
assert.isFunction(hoopy.grow)
assert.lengthOf(hoopy.grow, 1)
})
test('grow throws if by=0', () => {
assert.throws(() => hoopy.grow(0))
})
suite('assign to [0]:', () => {
setup(() => {
hoopy[0] = 'foo'
})
test('[0] is set correctly', () => {
assert.equal(hoopy[0], 'foo')
})
test('[1] is set correctly', () => {
assert.equal(hoopy[1], 'foo')
})
test('[-1] is set correctly', () => {
assert.equal(hoopy[-1], 'foo')
})
suite('assign to [1]:', () => {
setup(() => {
hoopy[1] = 'bar'
})
test('[0] is set correctly', () => {
assert.equal(hoopy[0], 'bar')
})
test('[1] is set correctly', () => {
assert.equal(hoopy[1], 'bar')
})
test('[-1] is set correctly', () => {
assert.equal(hoopy[-1], 'bar')
})
})
suite('grow, by=1:', () => {
setup(() => {
hoopy.grow(1)
})
test('length is correct', () => {
assert.equal(hoopy.length, 2)
})
test('[0] is set correctly', () => {
assert.equal(hoopy[0], 'foo')
})
test('[1] is undefined', () => {
assert.isUndefined(hoopy[1])
})
test('[-1] is undefined', () => {
assert.isUndefined(hoopy[-1])
})
})
})
})
suite('instantiate, size=2:', () => {
let hoopy
setup(() => {
hoopy = new Hoopy(2)
})
test('length is correct', () => {
assert.equal(hoopy.length, 2)
})
suite('assign to [0]:', () => {
setup(() => {
hoopy[0] = 'foo'
})
test('[0] is set correctly', () => {
assert.equal(hoopy[0], 'foo')
})
test('[1] is undefined', () => {
assert.isUndefined(hoopy[1])
})
test('[2] is set correctly', () => {
assert.equal(hoopy[2], 'foo')
})
test('[3] is undefined', () => {
assert.isUndefined(hoopy[3])
})
test('[-1] is undefined', () => {
assert.isUndefined(hoopy[-1])
})
suite('assign to [1]:', () => {
setup(() => {
hoopy[1] = 'bar'
})
test('[0] is set correctly', () => {
assert.equal(hoopy[0], 'foo')
})
test('[1] is set correctly', () => {
assert.equal(hoopy[1], 'bar')
})
test('[2] is set correctly', () => {
assert.equal(hoopy[2], 'foo')
})
test('[-1] is set correctly', () => {
assert.equal(hoopy[-1], 'bar')
})
suite('assign to [2]:', () => {
setup(() => {
hoopy[2] = 'baz'
})
test('[0] is set correctly', () => {
assert.equal(hoopy[0], 'baz')
})
test('[1] is set correctly', () => {
assert.equal(hoopy[1], 'bar')
})
test('[2] is set correctly', () => {
assert.equal(hoopy[2], 'baz')
})
test('[-1] is set correctly', () => {
assert.equal(hoopy[-1], 'bar')
})
suite('grow, by=1:', () => {
setup(() => {
hoopy.grow(1)
})
test('length is correct', () => {
assert.equal(hoopy.length, 3)
})
test('[0] is undefined', () => {
assert.isUndefined(hoopy[0])
})
test('[1] is set correctly', () => {
assert.equal(hoopy[1], 'bar')
})
test('[2] is set correctly', () => {
assert.equal(hoopy[2], 'baz')
})
test('[3] is undefined', () => {
assert.isUndefined(hoopy[3])
})
})
suite('grow, by=2:', () => {
setup(() => {
hoopy.grow(2)
})
test('length is correct', () => {
assert.equal(hoopy.length, 4)
})
test('[0] is undefined', () => {
assert.isUndefined(hoopy[0])
})
test('[1] is set correctly', () => {
assert.equal(hoopy[1], 'bar')
})
test('[2] is set correctly', () => {
assert.equal(hoopy[2], 'baz')
})
test('[3] is undefined', () => {
assert.isUndefined(hoopy[3])
})
test('[4] is undefined', () => {
assert.isUndefined(hoopy[4])
})
test('[5] is set correctly', () => {
assert.equal(hoopy[5], 'bar')
})
})
})
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment