UNPKG

mimicry-js

Version:

TS library for generating fake data for tests

2 lines (1 loc) 4.6 kB
"use strict";class t{constructor(t){this.value=t}}function n(t){if("object"!=typeof t||null===t||Array.isArray(t))return!1;const n=Object.getPrototypeOf(t);return null!==n&&n!==Object.prototype}function e(t){return"object"==typeof t&&null!==t&&"function"==typeof(null==t?void 0:t.next)}function r(t){return!(!t||"object"!=typeof t||Array.isArray(t)||n(t)||e(t)||"[object Object]"!==Object.prototype.toString.call(t))}function i(t,n){if(!r(t)||!r(n))return n;const e=Object.assign({},t);return Object.keys(n).forEach((t=>{const o=e[t],s=n[t];r(s)&&r(o)?e[t]=i(o,s):e[t]=s})),e}class o{constructor(t){this.generator=t}}const s=t=>{const n=t instanceof o;const e=r(t),i=function(t){return"function"==typeof t}(t);return{originFields:e?t:{},originFieldsFunction:i?t:null,originFieldsGenerator:n?t.generator:null}};class u{constructor(){this._listeners=new Set}listen(t){this._listeners.has(t)||this._listeners.add(t)}}class l extends u{constructor(){super()}reset(){for(const t of this._listeners.keys())t()}get listenersCount(){return this._listeners.size}}const c=function(o){const u=s(o.fields),c=o.traits,a=o.postBuild;let f,d=null,h=null;const p=new l,y=(t,n)=>{if(!r(t))return t;const e=function(t){var n;return null!==(n=null==t?void 0:t.overrides)&&void 0!==n?n:{}}(n),o=function(t){const n=null==t?void 0:t.traits;return Array.isArray(n)?n:n?[n]:[]}(n).reduce(((t,n)=>{var e;(null==c?void 0:c[n])||console.warn(`Trait "${String(n)}" is not specified in buildConfig!`);return i(t,null!==(e=(c?c[n]:{}).overrides)&&void 0!==e?e:{})}),{}),s=i(t,o),u=i(s,e);return l=u,a=t=>g(t),Object.keys(l).reduce(((t,n)=>(t[n]=a(l[n],n,t),t)),{});var l,a},v=t=>{if(null!==d)return d;const n={};for(const i in t){const o=t[i];if(e(o))n[i]=o;else if(r(o)){const t=v(o);Object.keys(t).length&&(n[i]=t)}}return d=n,d},g=i=>null==i?i:i instanceof t?i.value:function(t){return"function"==typeof t&&!/^class\s+/.test(Function.prototype.toString.call(t))}(i)?i():e(i)?i.next(p).value:n(i)?i:Array.isArray(i)?i.map((t=>g(t))):r(i)?y(i):i,x=t=>{if(!u.originFieldsGenerator)return;const n=(e=null==t?void 0:t.initialParameters,Array.isArray(e)?e:[]);var e;h=u.originFieldsGenerator(...n)},b=t=>{let n;if(u.originFieldsFunction){const t=u.originFieldsFunction(f);n=i(t,v(t))}else if(u.originFieldsGenerator){if(!h)throw Error("The fields GeneratorFunction isn't initialized!");const t=h.next(f).value;n=i(t,v(t))}else n=u.originFields;const e=y(n,t);f=e;const r=a?a(e):e;return(null==t?void 0:t.postBuild)?t.postBuild(r):r};return{one:t=>(x(t),b(t)),many:(t,n)=>(x(n),Array(t).fill(0).map((()=>b(n)))),reset:()=>{p.reset()}}},a=624;class f{constructor(t){this.MT=new Array(a),this.index=a,this.seed(t)}seed(t){this.MT[0]=t>>>0;for(let t=1;t<a;t++)this.MT[t]=1812433253*(this.MT[t-1]^this.MT[t-1]>>>30)+t>>>0;this.index=a}twist(){for(let t=0;t<a;t++){const n=(2147483648&this.MT[t])+(2147483647&this.MT[(t+1)%a]);this.MT[t]=this.MT[(t+397)%a]^n>>>1,n%2!=0&&(this.MT[t]^=2567483615)}this.index=0}random(){this.index>=a&&this.twist();let t=this.MT[this.index++];return t^=t>>>11,t^=t<<7&2636928640,t^=t<<15&4022730752,t^=t>>>18,(t>>>0)/4294967296}}function d(){return Math.floor(1e3*Math.random())}let h=d(),p=new f(h);function y(){return p.random()}function v(t,n){var e;if(t=void 0!==t&&void 0!==n?t:1,n=null!==(e=null!=n?n:t)&&void 0!==e?e:1e3,!Number.isInteger(t)||!Number.isInteger(n))throw new Error("int() expects integer arguments!");t>n&&([t,n]=[n,t]);const r=n-t+1;return Math.floor(y()*r)+t}function*g(t,...n){const e=Array.isArray(t)?t:[t,...n];for(;;)yield e[v(0,e.length-1)]}function x(t){let n,e=t;function r(){e=t}return{val:()=>e,set:t=>(e=t,e),use(t){if(!t)throw Error("An instance of `ResetSignal` was not provided!");return n!==t&&(n=t,n.listen(r)),n}}}exports.bool=function(){return g(!0,!1)},exports.build=c,exports.fixed=function(n){return new t(n)},exports.float=function(t,n){var e;return(t=void 0!==t&&void 0!==n?t:0)>(n=null!==(e=null!=n?n:t)&&void 0!==e?e:1)&&([t,n]=[n,t]),y()*(n-t)+t},exports.generate=function(t){return new o(t)},exports.getSeed=function(){return h},exports.int=v,exports.oneOf=g,exports.resetable=x,exports.seed=function(t=d()){h=t,p=new f(h)},exports.sequence=function*(t){const{val:n,set:e,use:r}=x(0);for(;;)r(yield t?t(e(n()+1)):e(n()+1))},exports.unique=function*(t,...n){const e=Array.isArray(t)?t:[t,...n],{val:r,set:i,use:o}=x(e.concat());for(;;){const t=r().concat();if(!t.length)throw new Error("No unique options left!");const n=v(0,t.length-1),e=t[n];t.splice(n,1),i(t),o(yield e)}},exports.withPrev=function(t){return function*(){let n;for(;;)n=t(n),yield n}()};