Skip to content
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@
"test:watch": "jest --updateSnapshot --watchAll"
},
"types": "dist/index.d.ts",
"version": "0.5.2"
"version": "0.6.0"
}
21 changes: 21 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export type {
ClosestKeyDesc,
Compared,
EqualityFn,
KeyType,
Node,
Expand All @@ -8,4 +10,23 @@ export type {
TrieableNode,
TrieableNodeKeyMapping
} from './main';

import {
bSearch,
getDescriptor,
isIterable,
lessThanValue,
sameValueZero,
toArray
} from './main';

export const util = {
bSearch,
defaultEqMatcher: sameValueZero,
defaultLtMatcher: lessThanValue,
getTypeName: getDescriptor,
isIterable,
toArray
};

export { default as default } from './main';
14 changes: 7 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export enum Compared {
LT = -1
};

interface ClosestKeyDesc {
export interface ClosestKeyDesc {
desc : Compared;
index : number;
};
Expand Down Expand Up @@ -61,7 +61,7 @@ const typeMapping = {
undefined: undefined
};

const getDescriptor : ( ( obj: any ) => string ) = (() => {
export const getDescriptor : ( ( obj: any ) => string ) = (() => {
const t = Object.prototype.toString;
return obj => {
const desc = t.call( obj );
Expand All @@ -70,7 +70,7 @@ const getDescriptor : ( ( obj: any ) => string ) = (() => {
})();

/** @throws { TypeError } */
const lessThanValue : EqualityFn = ( a, b ) => {
export const lessThanValue : EqualityFn = ( a, b ) => {
const typeA = typeof a;
const typeB = typeof b;
if( typeA === 'undefined' || a === null ) {
Expand All @@ -89,7 +89,7 @@ const lessThanValue : EqualityFn = ( a, b ) => {
};

/** Credit: curtesy of the lodash.eq() imnplementation */
const sameValueZero : EqualityFn = ( a, b ) => a === b || ( a !== a && b !== b );
export const sameValueZero : EqualityFn = ( a, b ) => a === b || ( a !== a && b !== b );

const TRIE_DESC = 'webKrafters.Trie';

Expand Down Expand Up @@ -785,7 +785,7 @@ function stringHash( key : string ) {
}
return hash;
};
function bSearch<T = unknown>(
export function bSearch<T = unknown>(
needle : T,
haystack : Array<T>,
compare : ( a, b ) => Compared = defaultComparator
Expand All @@ -807,14 +807,14 @@ function bSearch<T = unknown>(
return res;
}
function defaultComparator ( a, b ){ return a < b ? Compared.LT : a > b ? Compared.GT : Compared.EQ }
function isIterable( sequence ) {
export function isIterable( sequence ) {
if( sequence === null ) { return false }
if( typeof sequence[ Symbol.iterator ] === 'function' ) { return true }
/* pre-es6 support */
const type = getDescriptor( sequence );
return type === 'Array' || type === 'String';
}

function toArray<T>( sequence : Iterable<T> ) : Array<T> {
export function toArray<T>( sequence : Iterable<T> ) : Array<T> {
return Array.isArray( sequence ) ? sequence : [ ...sequence ];
}