Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions main.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare class ArrayKeyedMap<K, V> extends Map<K, V> {
hasPrefix(k: ReadonlyArray<any>): boolean;
}

export = ArrayKeyedMap;
5 changes: 5 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ module.exports = ArrayKeyedMap

function set (path, value) {
let map = this._root
if (!Array.isArray(path)) { path = [path] }
for (const item of path) {
let nextMap = map.get(item)
if (!nextMap) {
Expand All @@ -120,6 +121,7 @@ function set (path, value) {

function has (path) {
let map = this._root
if (!Array.isArray(path)) { path = [path] }
for (const item of path) {
const nextMap = map.get(item)
if (nextMap) {
Expand All @@ -133,6 +135,7 @@ function has (path) {

function get (path) {
let map = this._root
if (!Array.isArray(path)) { path = [path] }
for (const item of path) {
map = map.get(item)
if (!map) return undefined
Expand All @@ -147,6 +150,7 @@ function del (path) {
// if we delete something.
const stack = []

if (!Array.isArray(path)) { path = [path] }
for (const item of path) {
const nextMap = map.get(item)
if (nextMap) {
Expand Down Expand Up @@ -177,6 +181,7 @@ function del (path) {

function hasPrefix (path) {
let map = this._root
if (!Array.isArray(path)) { path = [path] }
for (const item of path) {
map = map.get(item)
if (!map) return false
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "2.1.2",
"description": "a map from arrays of values to values",
"main": "main.js",
"types": "main.d.ts",
"scripts": {
"test": "npm run test-code && npm run test-docs",
"test-docs": "txm readme.md",
Expand Down
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ test('set/get path len 1', (t) => {
t.end()
})

test('set/get non-array acts like len 1', (t) => {
const p = new AKM()
p.set('abc', true)
t.same(p.get(['abc']), true)
p.set(['def'], true)
t.same(p.get('def'), true)
t.end()
})

test('set/get path len 2', (t) => {
const p = new AKM()
p.set(['a', 'b'], true)
Expand Down Expand Up @@ -181,16 +190,28 @@ test('delete return value', t => {
t.end()
})

test('delete supports non-array key', t => {
const p = new AKM()

p.set(['x'], 'x')
t.same(p.delete('x'), true, 'delete returns true when entry existed')
t.same(p.delete('x'), false, 'delete returns false when no entry existed')

t.end()
})

test('has', (t) => {
const p = new AKM()

p.set(['a', 'b'], 'ab')

t.same(p.has(['a', 'b']), true)
t.same(p.has(['a']), false)
t.same(p.has('a'), false)

p.set(['a'], 'a')
t.same(p.has(['a']), true)
t.same(p.has('a'), true)

t.end()
})
Expand Down Expand Up @@ -342,14 +363,17 @@ test('hasPrefix', (t) => {
const p = new AKM()
p.set(['a', 'b', 'c'], 'abc')
p.set(['c'], 'c')
p.set([123, 456], 'd')

t.ok(p.hasPrefix([]))
t.ok(p.hasPrefix(['a']))
t.ok(p.hasPrefix(['a']))
t.ok(p.hasPrefix(['a', 'b']))
t.ok(p.hasPrefix(['a', 'b', 'c']))
t.ok(!p.hasPrefix(['a', 'b', 'c', 'd']))
t.ok(!p.hasPrefix(['b']))
t.ok(p.hasPrefix(['c']))
t.ok(p.hasPrefix(123))

t.end()
})
Expand Down