Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

digital-tree

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

digital-tree - npm Package Compare versions

Comparing version 0.0.1 to 1.0.0

35

index.js

@@ -9,2 +9,9 @@ var debug = require('debug')('digital-tree')

/**
* put something in the tree.
*
* @param {Array} key - each member in the array is a level in the tree
* @param {variant} value - the value to store
*
*/
Trie.prototype.put = function (key, value) {

@@ -26,2 +33,9 @@ debug('put( {%s}, {%s} )', key, value)

/**
* remove something from the tree
*
* @param {Array} key
*
* @return {Object} subtree that was removed
*/
Trie.prototype.remove = function (key) {

@@ -33,3 +47,3 @@ debug('remove( {%s} )', key)

// find the path, if its not found return
// find the path, return nothing if its not there
for (var i = 0; i < key.length; i++) {

@@ -45,6 +59,19 @@ var node = key[i]

if (parent)
delete parent[key[key.length - 1]]
var last = key[key.length - 1]
var subtree = parent[last]
if (parent) {
delete parent[last]
}
return subtree
}
/**
* get something from the tree
*
* @param {Array} key
*
* @return {variant} the value that was placed under that key
*/
Trie.prototype.get = function(key) {

@@ -67,2 +94,3 @@ debug('get( {%s} )', key)

/**
* Search for something in the tree
*

@@ -72,2 +100,3 @@ * @param key an array of tokens

*
* @return {Array} an array of arrays, each sub array contains the key and the value
*/

@@ -74,0 +103,0 @@ Trie.prototype.searchByPrefix = function(key, excludeKeys) {

2

package.json
{
"name": "digital-tree",
"version": "0.0.1",
"version": "1.0.0",
"description": "trie data structure",

@@ -5,0 +5,0 @@ "main": "index.js",

# digital tree
Trie data structure implementation
Trie data structure implementation
## Install
```
npm install --save digital-tree
```
## API
```javascript
var Trie = require('digital-tree')
var trie = new Trie()
```
### put(key, value)
Put something in the tree
```javascript
trie.puth(['a', 'path', 'to'], 'something')
```
### remove(key)
Remove something from the tree
given:
```json
{ "a": {
"path": {
"to": {
"$": "value"
}
}
}
}
```
```javascript
var subtree = trie.remove(['a', 'path'])
```
subtree will be:
```json
{ "to": { "$": "value" } }
```
Trie underlying data will look like this after the removal:
```json
{ "a": {}}
```
### get(key)
Get something from the tree
```javascript
var value = trie.get(['a', 'path', 'to'])
```
value will be the string "something"
### searchByPrefix(key, excludeEmptyKeys)
Search for all the entries under key
```javascript
var result = trie.searchByPrefix(['a', 'path'])
```
result will be:
```json
[ [ [ "a", "path", "to" ], "something" ] ]
```
TODO
- add benchmarks of other similar modules

@@ -11,10 +11,2 @@ var Trie = require('./index.js')

beforeEach(function () {
trie = new Trie()
trie.put(['a', 'b', 'c'], 'data')
trie.put(['a', 'b', 'd'], 'data1')
trie.put(['a'], 'data2')
trie.put(['a', 'd'], 'data3')
})
it('put()', function () {

@@ -30,4 +22,7 @@ assert.strictEqual(typeof trie._data.a, 'object')

it('remove()', function () {
trie.remove(['a', 'b', 'c'])
var subtree = trie.remove(['a', 'b', 'c'])
assert.strictEqual(trie._data.a.b.c, undefined)
assert.strictEqual(subtree.$, 'data')
})

@@ -54,3 +49,2 @@

var results = trie.searchByPrefix(['a', 'b'])
assert.deepEqual(results[0], [['a', 'b', 'c'], 'data'])

@@ -72,3 +66,3 @@ assert.deepEqual(results[1], [['a', 'b', 'd'], 'data1'])

})
describe('benchmark', function () {

@@ -110,3 +104,10 @@ var putBench

})
beforeEach(function () {
trie = new Trie()
trie.put(['a', 'b', 'c'], 'data')
trie.put(['a', 'b', 'd'], 'data1')
trie.put(['a'], 'data2')
trie.put(['a', 'd'], 'data3')
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc