@prismicio/mock
Version:
Generate mock Prismic documents, fields, Slices, and models for development and testing environments
1 lines • 3.29 kB
Source Map (JSON)
{"version":3,"file":"PRNG.cjs","names":[],"sources":["../../src/lib/PRNG.ts"],"sourcesContent":["/**\n * The following code is a modified version of Prando from `zeh/prando` on GitHub.\n *\n * Source: https://github.com/zeh/prando/blob/acc2a3c09df12a41b5c82fbf44e49e070e6f60ac/src/Prando.ts\n *\n * The MIT License (MIT)\n *\n * Copyright (c) 2016 Zeh Fernando\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and\n * associated documentation files (the \"Software\"), to deal in the Software without restriction,\n * including without limitation the rights to use, copy, modify, merge, publish, distribute,\n * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all copies or\n * substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT\n * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\n * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n */\n\nconst MIN = -2147483648 // Int32 min\nconst MAX = 2147483647 // Int32 max\n\nconst xorshift = (value: number): number => {\n\t// Xorshift*32\n\t// Based on George Marsaglia's work: http://www.jstatsoft.org/v08/i14/paper\n\tvalue ^= value << 13\n\tvalue ^= value >> 17\n\tvalue ^= value << 5\n\n\treturn value\n}\n\n/** Pseudorandom number generator. */\nexport class PRNG {\n\tprivate _value: number\n\n\t/**\n\t * Generate a new pseudo-random number generator.\n\t *\n\t * @param seed - A number or string seed that determines which pseudo-random number sequence will\n\t * be created.\n\t */\n\tconstructor(seed: number | string) {\n\t\tif (typeof seed === \"string\") {\n\t\t\tlet hash = 0\n\t\t\tif (seed) {\n\t\t\t\tconst l = seed.length\n\t\t\t\tfor (let i = 0; i < l; i++) {\n\t\t\t\t\thash = (hash << 5) - hash + seed.charCodeAt(i)\n\t\t\t\t\thash |= 0\n\t\t\t\t\thash = xorshift(hash)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthis._value = hash\n\t\t} else {\n\t\t\tthis._value = seed\n\t\t}\n\n\t\tif (this._value === 0) {\n\t\t\tthis._value = 1\n\t\t}\n\t}\n\n\t/**\n\t * Generates a pseudo-random number.\n\t *\n\t * @returns The generated pseudo-random number.\n\t */\n\tpublic next(): number {\n\t\tthis._value = xorshift(this._value)\n\n\t\treturn (this._value - MIN) / (MAX - MIN)\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,MAAM,MAAM;AACZ,MAAM,MAAM;AAEZ,MAAM,YAAY,UAA0B;AAG3C,UAAS,SAAS;AAClB,UAAS,SAAS;AAClB,UAAS,SAAS;AAElB,QAAO;;;AAIR,IAAa,OAAb,MAAkB;CACjB;;;;;;;CAQA,YAAY,MAAuB;AAClC,MAAI,OAAO,SAAS,UAAU;GAC7B,IAAI,OAAO;AACX,OAAI,MAAM;IACT,MAAM,IAAI,KAAK;AACf,SAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK;AAC3B,aAAQ,QAAQ,KAAK,OAAO,KAAK,WAAW,EAAE;AAC9C,aAAQ;AACR,YAAO,SAAS,KAAK;;;AAIvB,QAAK,SAAS;QAEd,MAAK,SAAS;AAGf,MAAI,KAAK,WAAW,EACnB,MAAK,SAAS;;;;;;;CAShB,OAAsB;AACrB,OAAK,SAAS,SAAS,KAAK,OAAO;AAEnC,UAAQ,KAAK,SAAS,QAAQ,MAAM"}