export function rnd ( seed = 0 ) { if ( seed === undefined ) { seed = Date . now ( ) ; } return ( ) => { seed = ( seed * 9301 + 49297 ) % 233280 ; return seed / 233280 ; } ; } export interface IGenerateDataOptions { default count ? : number ; default string ? : number ; default number ? : number ; default cat ? : number ; default categories ? : string [ ] ; default date ? : number ; seed ? : number ; default missing ? : number ; default missingString ? : number ; default missingNumber ? : number ; default missingCat ? : number ; default missingDate ? : number ; } export const DEFAULT_CATEGORIES = [ 'c1' , 'c2' , 'c3' ] ; export function generateData ( options = { } ) { const o = Object . assign ( { count : 100 , string : 1 , number : 1 , cat : 1 , categories : DEFAULT_CATEGORIES , date : 0 , seed : 0 , missing : 0 , missingString : options . missing == null ? 0 : options . missing , missingNumber : options . missing == null ? 0 : options . missing , missingCat : options . missing == null ? 0 : options . missing , missingDate : options . missing == null ? 0 : options . missing , } , options ) ; const arr = [ ] ; const s = rnd ( o . seed ) ; const isMissing = ( v ) => v > 0 && s ( ) <= v ; for ( let i = 0 ; i < o . count ; ++ i ) { const r = { } ; for ( let j = 0 ; j < o . string ; ++ j ) { const suffix = j === 0 ? '' : j ; r [ `string${ suffix }` ] = isMissing ( o . missingString ) ? null : `Row${ suffix } ${ i }` ; } for ( let j = 0 ; j < o . number ; ++ j ) { r [ `number${ j === 0 ? '' : j }` ] = isMissing ( o . missingNumber ) ? null : s ( ) * 10 ; } for ( let j = 0 ; j < o . cat ; ++ j ) { r [ `cat${ j === 0 ? '' : j }` ] = isMissing ( o . missingCat ) ? null : o . categories [ Math . floor ( s ( ) * o . categories . length ) ] ; } for ( let j = 0 ; j < o . date ; ++ j ) { r [ `date${ j === 0 ? '' : j }` ] = isMissing ( o . missingDate ) ? null : new Date ( Date . now ( ) - Math . floor ( s ( ) * 1000000000000 ) ) . toISOString ( ) ; } arr . push ( r ) ; } return arr ; }