-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
982 additions
and
704 deletions.
There are no files selected for viewing
This file contains 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 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
44 changes: 44 additions & 0 deletions
44
client/src/plugins/dataTypes/NormalDistribution/NormalDistribution.generate.ts
This file contains 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,44 @@ | ||
import { DTGenerateResult, DTGenerationData } from '~types/dataTypes'; | ||
|
||
export const generate = ({ rowState }: DTGenerationData): DTGenerateResult => { | ||
const { mean, sigma, precision } = rowState; | ||
|
||
return { | ||
display: gaussMs(mean, sigma).toFixed(precision) | ||
}; | ||
}; | ||
|
||
// adjust our gaussian random to fit the mean and standard deviation. The division by 4 is an arbitrary value to help | ||
// fit the distribution within our required range, and gives a best fit for stddev = 1.0 | ||
export const gaussMs = (mean: number, stddev: number): number => gauss() * (stddev / 4) + mean; | ||
|
||
let useExists = false; | ||
let useValue: number; | ||
|
||
export const gauss = (): number => { | ||
if (useExists) { | ||
// use value from a previous call to this function | ||
useExists = false; | ||
return useValue; | ||
} else { | ||
// polar form of the Box-Muller transformation | ||
let w = 2.0; | ||
let x = 0; | ||
let y = 0; | ||
while (w >= 1 || w === 0.0) { | ||
x = randomPN(); | ||
y = randomPN(); | ||
w = (x * x) + (y * y); | ||
} | ||
w = Math.sqrt((-2.0 * Math.log(w)) / w); | ||
|
||
// set value for next call to this function | ||
useValue = y * w; | ||
useExists = true; | ||
|
||
return x * w; | ||
} | ||
}; | ||
|
||
// returns random number with a flat distribution from -1 to 1 inclusive | ||
export const randomPN = (): number => (2.0 * Math.random()) - 1.0; |
48 changes: 3 additions & 45 deletions
48
client/src/plugins/dataTypes/NormalDistribution/NormalDistribution.worker.ts
This file contains 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 |
---|---|---|
@@ -1,48 +1,6 @@ | ||
import { DTGenerateResult, DTWorkerOnMessage } from '~types/dataTypes'; | ||
import { DTWorkerOnMessage } from '~types/dataTypes'; | ||
import { generate } from './NormalDistribution.generate'; | ||
|
||
export const onmessage = (e: DTWorkerOnMessage) => { | ||
postMessage(generate(e.data.rowState)); | ||
postMessage(generate(e.data)); | ||
}; | ||
|
||
export const generate = (rowState: any): DTGenerateResult => { | ||
const { mean, sigma, precision } = rowState; | ||
|
||
return { | ||
display: gaussMs(mean, sigma).toFixed(precision) | ||
}; | ||
}; | ||
|
||
// adjust our gaussian random to fit the mean and standard deviation. The division by 4 is an arbitrary value to help | ||
// fit the distribution within our required range, and gives a best fit for stddev = 1.0 | ||
export const gaussMs = (mean: number, stddev: number): number => gauss() * (stddev / 4) + mean; | ||
|
||
let useExists = false; | ||
let useValue: number; | ||
|
||
export const gauss = (): number => { | ||
if (useExists) { | ||
// use value from a previous call to this function | ||
useExists = false; | ||
return useValue; | ||
} else { | ||
// polar form of the Box-Muller transformation | ||
let w = 2.0; | ||
let x = 0; | ||
let y = 0; | ||
while (w >= 1 || w === 0.0) { | ||
x = randomPN(); | ||
y = randomPN(); | ||
w = (x * x) + (y * y); | ||
} | ||
w = Math.sqrt((-2.0 * Math.log(w)) / w); | ||
|
||
// set value for next call to this function | ||
useValue = y * w; | ||
useExists = true; | ||
|
||
return x * w; | ||
} | ||
}; | ||
|
||
// returns random number with a flat distribution from -1 to 1 inclusive | ||
export const randomPN = (): number => (2.0 * Math.random()) - 1.0; |
This file contains 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 |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import { DTBundle } from '~types/dataTypes'; | ||
import { initialState, Options, getMetadata } from './NormalDistribution'; | ||
import { generate } from './NormalDistribution.generate'; | ||
|
||
export { NormalDistributionState as GenerationOptionsType } from './NormalDistribution'; | ||
|
||
const bundle: DTBundle = { | ||
initialState, | ||
Options, | ||
getMetadata | ||
getMetadata, | ||
generate | ||
}; | ||
|
||
export default bundle; |
10 changes: 10 additions & 0 deletions
10
client/src/plugins/dataTypes/NumberRange/NumberRange.generate.ts
This file contains 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,10 @@ | ||
import { DTGenerateResult, DTGenerationData } from '~types/dataTypes'; | ||
import { WorkerUtils } from "~utils/workerUtils"; | ||
|
||
export const generate = ({ rowState }: DTGenerationData, utils: WorkerUtils): DTGenerateResult => { | ||
const { min, max } = rowState; | ||
|
||
return { | ||
display: utils.randomUtils.getRandomNum(min, max) | ||
}; | ||
}; |
This file contains 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 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
89 changes: 89 additions & 0 deletions
89
client/src/plugins/dataTypes/OrganizationNumber/OrganizationNumber.generate.ts
This file contains 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,89 @@ | ||
import { DTGenerateResult, DTGenerationData } from '~types/dataTypes'; | ||
import { WorkerUtils } from '~utils/workerUtils'; | ||
|
||
// data: GenerationData | ||
export const generate = (data: DTGenerationData, utils: WorkerUtils): DTGenerateResult => { | ||
return { display: '' }; | ||
}; | ||
|
||
|
||
/* | ||
private $generatedOrgNrs = array(); | ||
static $sep = "-"; | ||
* Generate a random personal number, and return the display string and additional meta data for use | ||
* by any other Data Type. | ||
public function generate($generator, $generationContextData) { | ||
$generationOptions = $generationContextData["generationOptions"]; | ||
// Default, 10 siffers + '-' | ||
// TODO: support several countries? | ||
static::$sep = self::getOrganisationNumberSeparator($generationOptions["cc_separator"]); | ||
$orgnr = $this->generateRandomSwedishOrganisationNumber(static::$sep); | ||
// pretty sodding unlikely, but just in case! | ||
while (in_array($orgnr, $this->generatedOrgNrs)) { | ||
$orgnr = $this->generateRandomSwedishOrganisationNumber(static::$sep); | ||
} | ||
$this->generatedOrgNrs[] = $orgnr; | ||
return array( | ||
"display" => $orgnr | ||
); | ||
} | ||
// TODO: add support for separator | ||
// TODO: add support for organisation numbers | ||
private static function generateRandomSwedishOrganisationNumber($sep) { | ||
$new_str = ""; | ||
$rand = 0; | ||
$cnt = 11; // 10 siffers + 1 increment for separator | ||
for ($i=0; $i<$cnt; $i++) { | ||
switch ($i) { | ||
case 0: | ||
$rand = mt_rand(0, 99); | ||
$new_str .= sprintf("%02d", $rand); | ||
break; | ||
case 2: | ||
$rand = mt_rand(20, 99); | ||
$new_str .= sprintf("%02d", $rand); | ||
break; | ||
case 4: | ||
$rand = mt_rand(0, 99); | ||
$new_str .= sprintf("%02d", $rand); | ||
break; | ||
case 6: | ||
$new_str .= $sep; | ||
break; | ||
case 7: | ||
$rand = mt_rand(0, 999); | ||
$new_str .= sprintf("%03d", $rand); | ||
break; | ||
case 10: | ||
// Same calculation as for personal numbers | ||
// TODO: move to Utils?? | ||
$ctrl = DataType_PersonalNumber::recalcCtrl($new_str . "0", $sep); | ||
$new_str .= sprintf("%01d", $ctrl); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
return $new_str; | ||
} | ||
private static function getOrganisationNumberSeparator($separators) { | ||
$separatorList = explode("|", $separators); | ||
$chosenSep = $separatorList[rand(0, count($separatorList)-1)]; | ||
// if no separator was entered use '' as default | ||
if ($separators == "") { | ||
$chosenSep = ""; | ||
} | ||
return $chosenSep; | ||
} | ||
*/ |
94 changes: 10 additions & 84 deletions
94
client/src/plugins/dataTypes/OrganizationNumber/OrganizationNumber.worker.ts
This file contains 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 |
---|---|---|
@@ -1,88 +1,14 @@ | ||
import { DTGenerateResult } from '~types/dataTypes'; | ||
import utils from '../../../utils'; | ||
import { DTWorkerOnMessage } from '~types/dataTypes'; | ||
import { generate } from './OrganizationNumber.generate'; | ||
|
||
// data: GenerationData | ||
export const generate = (): DTGenerateResult => { | ||
return { display: '' }; | ||
}; | ||
|
||
|
||
/* | ||
private $generatedOrgNrs = array(); | ||
static $sep = "-"; | ||
* Generate a random personal number, and return the display string and additional meta data for use | ||
* by any other Data Type. | ||
public function generate($generator, $generationContextData) { | ||
$generationOptions = $generationContextData["generationOptions"]; | ||
let utilsLoaded = false; | ||
|
||
// Default, 10 siffers + '-' | ||
// TODO: support several countries? | ||
static::$sep = self::getOrganisationNumberSeparator($generationOptions["cc_separator"]); | ||
$orgnr = $this->generateRandomSwedishOrganisationNumber(static::$sep); | ||
// pretty sodding unlikely, but just in case! | ||
while (in_array($orgnr, $this->generatedOrgNrs)) { | ||
$orgnr = $this->generateRandomSwedishOrganisationNumber(static::$sep); | ||
} | ||
$this->generatedOrgNrs[] = $orgnr; | ||
return array( | ||
"display" => $orgnr | ||
); | ||
export const onmessage = (e: DTWorkerOnMessage) => { | ||
if (!utilsLoaded) { | ||
importScripts(e.data.workerUtilsUrl); | ||
utilsLoaded = true; | ||
} | ||
|
||
// TODO: add support for separator | ||
// TODO: add support for organisation numbers | ||
private static function generateRandomSwedishOrganisationNumber($sep) { | ||
$new_str = ""; | ||
$rand = 0; | ||
$cnt = 11; // 10 siffers + 1 increment for separator | ||
for ($i=0; $i<$cnt; $i++) { | ||
switch ($i) { | ||
case 0: | ||
$rand = mt_rand(0, 99); | ||
$new_str .= sprintf("%02d", $rand); | ||
break; | ||
case 2: | ||
$rand = mt_rand(20, 99); | ||
$new_str .= sprintf("%02d", $rand); | ||
break; | ||
case 4: | ||
$rand = mt_rand(0, 99); | ||
$new_str .= sprintf("%02d", $rand); | ||
break; | ||
case 6: | ||
$new_str .= $sep; | ||
break; | ||
case 7: | ||
$rand = mt_rand(0, 999); | ||
$new_str .= sprintf("%03d", $rand); | ||
break; | ||
case 10: | ||
// Same calculation as for personal numbers | ||
// TODO: move to Utils?? | ||
$ctrl = DataType_PersonalNumber::recalcCtrl($new_str . "0", $sep); | ||
$new_str .= sprintf("%01d", $ctrl); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
return $new_str; | ||
} | ||
private static function getOrganisationNumberSeparator($separators) { | ||
$separatorList = explode("|", $separators); | ||
$chosenSep = $separatorList[rand(0, count($separatorList)-1)]; | ||
// if no separator was entered use '' as default | ||
if ($separators == "") { | ||
$chosenSep = ""; | ||
} | ||
return $chosenSep; | ||
} | ||
*/ | ||
postMessage(generate(e.data, utils)); | ||
}; |
Oops, something went wrong.