UNPKG

@prismicio/mock

Version:

Generate mock Prismic documents, fields, Slices, and models for development and testing environments

1 lines 3.45 kB
{"version":3,"file":"PRNG.cjs","sources":["../../../src/lib/PRNG.ts"],"sourcesContent":["/**\n * The following code is a modified version of Prando from `zeh/prando` on\n * GitHub.\n *\n * Source:\n * 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\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * 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\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * 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\n * 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/**\n * Pseudorandom number generator.\n */\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\n\t * number sequence will 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"],"names":[],"mappings":";;;;;;;;AA8BA,MAAM,MAAM;AACZ,MAAM,MAAM;AAEZ,MAAM,WAAW,CAAC,UAAyB;AAG1C,WAAS,SAAS;AAClB,WAAS,SAAS;AAClB,WAAS,SAAS;AAEX,SAAA;AACR;MAKa,KAAI;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAShB,YAAY,MAAqB;AARzB;AASH,QAAA,OAAO,SAAS,UAAU;AAC7B,UAAI,OAAO;AACX,UAAI,MAAM;AACT,cAAM,IAAI,KAAK;AACf,iBAAS,IAAI,GAAG,IAAI,GAAG,KAAK;AAC3B,kBAAQ,QAAQ,KAAK,OAAO,KAAK,WAAW,CAAC;AACrC,kBAAA;AACR,iBAAO,SAAS,IAAI;AAAA,QACrB;AAAA,MACD;AAEA,WAAK,SAAS;AAAA,IAAA,OACR;AACN,WAAK,SAAS;AAAA,IACf;AAEI,QAAA,KAAK,WAAW,GAAG;AACtB,WAAK,SAAS;AAAA,IACf;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,OAAI;AACL,SAAA,SAAS,SAAS,KAAK,MAAM;AAE1B,YAAA,KAAK,SAAS,QAAQ,MAAM;AAAA,EACrC;AACA;;"}