From 4d64698eff1569cd199831301cb694eccc55b87e Mon Sep 17 00:00:00 2001 From: Michael Zabka Date: Sun, 22 Mar 2015 18:58:43 +0100 Subject: [PATCH] Add npm library object-hash --- object-hash/object-hash-tests.ts | 48 +++++++++++++++++++++++++++++ object-hash/object-hash.d.ts | 52 ++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 object-hash/object-hash-tests.ts create mode 100644 object-hash/object-hash.d.ts diff --git a/object-hash/object-hash-tests.ts b/object-hash/object-hash-tests.ts new file mode 100644 index 0000000000..e72c9e4891 --- /dev/null +++ b/object-hash/object-hash-tests.ts @@ -0,0 +1,48 @@ +/// + +import hash = require('object-hash'); + +var hashed: string; + +var obj = { any: true }; + +// hash object +hashed = hash(obj); + +hashed = hash.sha1(obj); +hashed = hash.keys(obj); +hashed = hash.MD5(obj); +hashed = hash.keysMD5(obj); + +var options = { + algorithm: 'md5', + encoding: 'utf8', + excludeValues: true +}; + +hashed = hash(obj, options); + +// HashTable +var table: ObjectHash.HashTable; +table = hash.HashTable(); +table = hash.HashTable(options); + +table = table.add(obj); +table = table.add(obj, obj); +table = table.remove(obj); +table = table.remove(obj, obj); + +var has: boolean = table.hasKey('whatEver'); +var value: any = table.getValue('whatEver'); +var count: number = table.getCount('whatEver'); + +var tableObject = table.table(); +tableObject['whatEver'].value; +tableObject['whatEver'].count; + +var tableArray = table.toArray(); +tableArray.shift().value; +tableArray.pop().count; +tableArray[2].hash; + +table = table.reset(); diff --git a/object-hash/object-hash.d.ts b/object-hash/object-hash.d.ts new file mode 100644 index 0000000000..8faf058dae --- /dev/null +++ b/object-hash/object-hash.d.ts @@ -0,0 +1,52 @@ +// Type definitions for object-hash v0.5.0 +// Project: https://github.com/puleos/object-hash +// Definitions by: Michael Zabka +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module ObjectHash { + export interface IOptions { + algorithm?: string; + encoding?: string; + excludeValues?: boolean; + } + + interface HashTableItem { + value: any; + count: number; + } + + interface HashTableItemWithKey extends HashTableItem { + hash: string; + } + + export interface HashTable { + add(...values: any[]): HashTable; + remove(...values: any[]): HashTable; + hasKey(key: string): boolean; + getValue(key: string): any; + getCount(key: string): number; + table(): { [key: string]: HashTableItem }; + toArray(): HashTableItemWithKey[]; + reset(): HashTable; + } + + export interface HashTableStatic { + (options?: IOptions): HashTable; + } + + export interface Hash { + (object: any, options?: IOptions): string; + sha1(object: any): string; + keys(object: any): string; + MD5(object: any): string; + keysMD5(object: any): string; + HashTable: HashTableStatic; + } + + export var HashStatic: Hash; +} + +declare module 'object-hash' { + import HashStatic = ObjectHash.HashStatic; + export = HashStatic; +}