forked from ton-core/ton-core
-
Notifications
You must be signed in to change notification settings - Fork 46
feature(clean-deps): removed prando dependency + made symbolInspect private #133
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // https://nodejs.org/api/util.html#utilinspectcustom | ||
| export const inspectSymbol = Symbol.for("nodejs.util.inspect.custom"); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| /** | ||
| The MIT License (MIT) | ||
|
|
||
| Copyright (c) 2016 Zeh Fernando | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. | ||
| */ | ||
|
|
||
| export class Prando { | ||
| private static readonly MIN: number = -2147483648; // Int32 min | ||
| private static readonly MAX: number = 2147483647; // Int32 max | ||
| private _seed: number; | ||
| private _value = NaN; | ||
|
|
||
| // ================================================================================================================ | ||
| // CONSTRUCTOR ---------------------------------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Generate a new Prando pseudo-random number generator. | ||
| * | ||
| * @param seed - A number or string seed that determines which pseudo-random number sequence will be created. Defaults to a random seed based on `Math.random()`. | ||
| */ | ||
| constructor(seed?: number | string) { | ||
| if (typeof seed === "string") { | ||
| // String seed | ||
| this._seed = this.hashCode(seed); | ||
| } else if (typeof seed === "number") { | ||
| // Numeric seed | ||
| this._seed = this.getSafeSeed(seed); | ||
| } else { | ||
| // Pseudo-random seed | ||
| this._seed = this.getSafeSeed( | ||
| Prando.MIN + | ||
| Math.floor((Prando.MAX - Prando.MIN) * Math.random()), | ||
| ); | ||
| } | ||
| this.reset(); | ||
| } | ||
|
|
||
| // ================================================================================================================ | ||
| // PUBLIC INTERFACE ----------------------------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Generates a pseudo-random number between a lower (inclusive) and a higher (exclusive) bounds. | ||
| * | ||
| * @param min - The minimum number that can be randomly generated. | ||
| * @param pseudoMax - The maximum number that can be randomly generated (exclusive). | ||
| * @return The generated pseudo-random number. | ||
| */ | ||
| public next(min = 0, pseudoMax = 1): number { | ||
| this.recalculate(); | ||
| return this.map(this._value, Prando.MIN, Prando.MAX, min, pseudoMax); | ||
| } | ||
|
|
||
| /** | ||
| * Generates a pseudo-random integer number in a range (inclusive). | ||
| * | ||
| * @param min - The minimum number that can be randomly generated. | ||
| * @param max - The maximum number that can be randomly generated. | ||
| * @return The generated pseudo-random number. | ||
| */ | ||
| public nextInt(min = 10, max = 100): number { | ||
| this.recalculate(); | ||
| return Math.floor( | ||
| this.map(this._value, Prando.MIN, Prando.MAX, min, max + 1), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Generates a pseudo-random string sequence of a particular length from a specific character range. | ||
| * | ||
| * Note: keep in mind that creating a random string sequence does not guarantee uniqueness; there is always a | ||
| * 1 in (char_length^string_length) chance of collision. For real unique string ids, always check for | ||
| * pre-existing ids, or employ a robust GUID/UUID generator. | ||
| * | ||
| * @param length - Length of the string to be generated. | ||
| * @param chars - Characters that are used when creating the random string. Defaults to all alphanumeric chars (A-Z, a-z, 0-9). | ||
| * @return The generated string sequence. | ||
| */ | ||
| public nextString( | ||
| length = 16, | ||
| chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", | ||
| ): string { | ||
| let str = ""; | ||
| while (str.length < length) { | ||
| str += this.nextChar(chars); | ||
| } | ||
| return str; | ||
| } | ||
|
|
||
| /** | ||
| * Generates a pseudo-random string of 1 character specific character range. | ||
| * | ||
| * @param chars - Characters that are used when creating the random string. Defaults to all alphanumeric chars (A-Z, a-z, 0-9). | ||
| * @return The generated character. | ||
| */ | ||
| public nextChar( | ||
| chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", | ||
| ): string { | ||
| return chars.substr(this.nextInt(0, chars.length - 1), 1); | ||
| } | ||
|
|
||
| /** | ||
| * Picks a pseudo-random item from an array. The array is left unmodified. | ||
| * | ||
| * Note: keep in mind that while the returned item will be random enough, picking one item from the array at a time | ||
| * does not guarantee nor imply that a sequence of random non-repeating items will be picked. If you want to | ||
| * *pick items in a random order* from an array, instead of *pick one random item from an array*, it's best to | ||
| * apply a *shuffle* transformation to the array instead, then read it linearly. | ||
| * | ||
| * @param array - Array of any type containing one or more candidates for random picking. | ||
| * @return An item from the array. | ||
| */ | ||
| public nextArrayItem<T>(array: T[]): T { | ||
| return array[this.nextInt(0, array.length - 1)]; | ||
| } | ||
|
|
||
| /** | ||
| * Generates a pseudo-random boolean. | ||
| * | ||
| * @return A value of true or false. | ||
| */ | ||
| public nextBoolean(): boolean { | ||
| this.recalculate(); | ||
| return this._value > 0.5; | ||
| } | ||
|
|
||
| /** | ||
| * Skips ahead in the sequence of numbers that are being generated. This is equivalent to | ||
| * calling next() a specified number of times, but faster since it doesn't need to map the | ||
| * new random numbers to a range and return it. | ||
| * | ||
| * @param iterations - The number of items to skip ahead. | ||
| */ | ||
| public skip(iterations = 1): void { | ||
| while (iterations-- > 0) { | ||
| this.recalculate(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Reset the pseudo-random number sequence back to its starting seed. Further calls to next() | ||
| * will then produce the same sequence of numbers it had produced before. This is equivalent to | ||
| * creating a new Prando instance with the same seed as another Prando instance. | ||
| * | ||
| * Example: | ||
| * let rng = new Prando(12345678); | ||
| * console.log(rng.next()); // 0.6177754114889017 | ||
| * console.log(rng.next()); // 0.5784605181725837 | ||
| * rng.reset(); | ||
| * console.log(rng.next()); // 0.6177754114889017 again | ||
| * console.log(rng.next()); // 0.5784605181725837 again | ||
| */ | ||
| public reset(): void { | ||
| this._value = this._seed; | ||
| } | ||
|
|
||
| // ================================================================================================================ | ||
| // PRIVATE INTERFACE ---------------------------------------------------------------------------------------------- | ||
|
|
||
| private recalculate(): void { | ||
| this._value = this.xorshift(this._value); | ||
| } | ||
|
|
||
| private xorshift(value: number): number { | ||
| // Xorshift*32 | ||
| // Based on George Marsaglia's work: http://www.jstatsoft.org/v08/i14/paper | ||
| value ^= value << 13; | ||
| value ^= value >> 17; | ||
| value ^= value << 5; | ||
| return value; | ||
| } | ||
|
|
||
| private map( | ||
| val: number, | ||
| minFrom: number, | ||
| maxFrom: number, | ||
| minTo: number, | ||
| maxTo: number, | ||
| ): number { | ||
| return ( | ||
| ((val - minFrom) / (maxFrom - minFrom)) * (maxTo - minTo) + minTo | ||
| ); | ||
| } | ||
|
|
||
| private hashCode(str: string): number { | ||
| let hash = 0; | ||
| if (str) { | ||
| const l = str.length; | ||
| for (let i = 0; i < l; i++) { | ||
| hash = (hash << 5) - hash + str.charCodeAt(i); | ||
| hash |= 0; | ||
| hash = this.xorshift(hash); | ||
| } | ||
| } | ||
| return this.getSafeSeed(hash); | ||
| } | ||
|
|
||
| private getSafeSeed(seed: number): number { | ||
| if (seed === 0) return 1; | ||
| return seed; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.