Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript & JavaScript doc types #11

Open
amatsagu opened this issue Jul 10, 2021 · 0 comments
Open

TypeScript & JavaScript doc types #11

amatsagu opened this issue Jul 10, 2021 · 0 comments

Comments

@amatsagu
Copy link

Currently we don't have any definition file for this package. How about adding them?

I've made my own implementation and it works good enough to be used in global:

declare module 'megahash' {
    export default class MegaHash<K, V> {
        /**
         * Set or replace one key/value in the hash.
         * Ideally both key and value are passed as Buffers, as this provides the highest performance.
         * Most built-in data types are supported of course, but they are converted to buffers one way or the other.
         * It actually returns a number that means type of result: 0 = error (out of memory), 1 = success, 2 = success (replaced existing value)
         * @param {V} key unique key
         * @param {K} value value to store
         */
        set(key: K, value:V ): 0 | 1 | 2

        /**
         * Fetch a value given a key.
         * Since the value data type is stored internally as a flag with the raw data, this is used to convert the buffer back to the original type when the key is fetched.
         * So if you store a string then fetch it, it'll come back as a string. If there's no such value - it gonna reply back with undefined.
         * @param {K} key unique key
         */
        get(key: K): V

        /**
         * Tests given key. Returns true if there is a some value connected with this key and false otherwise.
         * @param {K} key unique key
         */
        has(key: K): boolean

        /**
         * Deletes key & connected to it value from hash. Returns true if it finds value and deletes it, false otherwise.
         * @param {K} key unique key
         */
        delete (key: K): boolean

        /**
         * Delete all keys from the hash, effectively freeing all memory (except for indexes).
         * You can also optionally pass in one or two 8-bit unsigned integers to this method, to clear an arbitrary "slice" of the total keys. This is an advanced trick,
         * only used for clearing out extremely large hashes in small chunks, as to not hang the main thread.
         * Pass in one number between 0 - 256 to clear a "thick slice" (approximately 1/256 of total keys).
         * @param {number} from 
         * @param {number} to 
         */
        clear (from?: number, to?: number): void

        /**
         * Returns a number of keys currently allocated in mega hash (map).
         */
        length (): number

        /**
         * Fetch statistics about the current hash, including the number of keys, total data size in memory, and more. The return value is a native Node.js object with several properties populated.
         */
        stats (): MegaHashStatistics
    }

    interface MegaHashStatistics {
        numKeys: number
        dataSize: number
        indexSize: number
        metaSize: number
        numIndexes: number
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant