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

quick-trie

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

quick-trie - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

2

package.json

@@ -44,3 +44,3 @@ {

},
"version": "1.0.2"
"version": "1.1.0"
}

@@ -57,8 +57,19 @@ import { init, add, get, search } from './trie';

expect(hello).toEqual([1]);
expect(world.sort()).toEqual([1, 2].sort());
expect(lo).toEqual([1]);
expect(rl.sort()).toEqual([1, 2].sort());
expect(hello).toEqual([{ key: 'Hello World', value: 1 }]);
expect(world).toEqual([
{ key: 'World Best', value: 2 },
{ key: 'Hello World', value: 1 },
]);
expect(lo).toEqual([{ key: 'Hello World', value: 1 }]);
expect(rl).toEqual([
{ key: 'Hello World', value: 1 },
{ key: 'World Best', value: 2 },
]);
expect(lr).toEqual([]);
expect(be.sort()).toEqual([2, 3].sort());
expect(be.sort()).toEqual(
[
{ key: 'Beer', value: 3 },
{ key: 'World Best', value: 2 },
].sort(),
);
});

@@ -65,0 +76,0 @@

@@ -1,2 +0,2 @@

import { Node, TrieConfig } from './types';
import { Node, TrieConfig, SearchResult } from './types';

@@ -12,3 +12,7 @@ export const init = <T>(

export const add = <T>(node: Node<T>, key: string, value: T): Node<T> => {
export const add = <T>(node: Node<T>, key: string, value: T): void => {
addRecursively(node, key, key, value);
};
const addRecursively = <T>(node: Node<T>, key: string, originalKey: string, value: T): Node<T> => {
if (key.length === 0) {

@@ -22,3 +26,3 @@ return node;

if (matchChild) {
return add(matchChild, key.slice(1, key.length), value);
return addRecursively(matchChild, key.slice(1, key.length), originalKey, value);
} else {

@@ -29,6 +33,7 @@ const newNode = {

ignoreCasing: node.ignoreCasing,
key: key.length === 1 && originalKey,
value: key.length === 1 && value,
};
node.children[cultureAwareKey] = newNode;
return add(newNode, key.slice(1, key.length), value);
return addRecursively(newNode, key.slice(1, key.length), originalKey, value);
}

@@ -50,6 +55,10 @@ };

export const search = <T>(node: Node<T>, keyword: string): T[] =>
searchRecursively(node, keyword, keyword);
export const search = <T>(node: Node<T>, keyword: string): SearchResult<T>[] =>
searchRecursively(node, keyword, keyword).map(({ key, value }) => ({ key, value }));
const searchRecursively = <T>(node: Node<T>, keyword: string, originalKeyword: string): T[] => {
const searchRecursively = <T>(
node: Node<T>,
keyword: string,
originalKeyword: string,
): SearchResult<T>[] => {
let results = [];

@@ -86,3 +95,3 @@

originalKeyword: string,
): T[] => {
): SearchResult<T>[] => {
const childrenResults = Object.entries(node.children)

@@ -97,7 +106,8 @@ .map(([key, child]) =>

const applyEndWildcard = <T>(node: Node<T>): T[] => {
const applyEndWildcard = <T>(node: Node<T>): SearchResult<T>[] => {
let fetchResult = [];
if (node.value) {
fetchResult.push(node.value);
const { key, value } = node;
fetchResult.push({ key, value });
} else {

@@ -112,6 +122,7 @@ Object.values(node.children).forEach((child) => {

const getCultureAwareKey = <T>(node: Node<T>, key: string): string =>
node.ignoreCasing ? key[0].toLowerCase() : key[0];
const matchNext = <T>(matchChild: Node<T>, keyword: string, originalKeyword: string): T[] => {
const matchNext = <T>(
matchChild: Node<T>,
keyword: string,
originalKeyword: string,
): SearchResult<T>[] => {
let result = searchRecursively(matchChild, keyword.slice(1, keyword.length), originalKeyword);

@@ -127,1 +138,4 @@

};
const getCultureAwareKey = <T>(node: Node<T>, key: string): string =>
node.ignoreCasing ? key[0].toLowerCase() : key[0];
export interface Node<T> {
parent?: Node<T>;
children: { [key: string]: Node<T> };
key?: string;
value?: T;

@@ -8,4 +9,9 @@ ignoreCasing: boolean;

export interface SearchResult<T> {
key: string;
value: T;
}
export interface TrieConfig {
ignoreCasing: boolean;
}
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