Skip to content

Commit

Permalink
fix: number random generator with step and min/max
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Feb 12, 2020
1 parent f291e4f commit c42d7e4
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 46 deletions.
11 changes: 1 addition & 10 deletions core/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,7 @@
"docz-rollup": "^2.1.0",
"eslint": "^6.5.1",
"eslint-config-docz-ts": "^2.1.0",
"jest": "^24.9.0",
"rollup-plugin-alias": "^1.5.2",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.0.1",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-peer-deps-external": "^2.2.0",
"rollup-plugin-postcss": "^2.0.3",
"rollup-plugin-progress": "^1.1.1",
"rollup-plugin-typescript2": "^0.22.0"
"jest": "^24.9.0"
},
"publishConfig": {
"access": "public"
Expand Down
24 changes: 20 additions & 4 deletions core/core/src/randomizeData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ControlTypes,
ComponentControlNumber,
ComponentControlOptions,
} from '@component-controls/specification';
import { LoadedComponentControls } from './utils';
Expand Down Expand Up @@ -81,12 +82,27 @@ export const randomizeData = (
value: faker.random.boolean(),
};
case ControlTypes.NUMBER:
const step: number = control
? (control as ComponentControlNumber).step || 1
: 1;

const randomNumber: number = Math.max(
Math.min(
faker.random.number({
min:
(control as ComponentControlNumber).min ||
(control.value as number) / 2,
max:
(control as ComponentControlNumber).max ||
(control.value as number) * 2,
}),
(control as ComponentControlNumber).max || Infinity,
),
(control as ComponentControlNumber).min || -Infinity,
);
return {
name,
value: faker.random.number({
min: (control.value as number) / 2,
max: (control.value as number) * 2,
}),
value: Math.ceil(randomNumber / step) * step,
};
case ControlTypes.OBJECT: {
if (typeof control.value === 'object') {
Expand Down
11 changes: 1 addition & 10 deletions core/editors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,7 @@
"docz-rollup": "^2.1.0",
"eslint": "^6.5.1",
"eslint-config-docz-ts": "^2.1.0",
"jest": "^24.9.0",
"rollup-plugin-alias": "^1.5.2",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.0.1",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-peer-deps-external": "^2.2.0",
"rollup-plugin-postcss": "^2.0.3",
"rollup-plugin-progress": "^1.1.1",
"rollup-plugin-typescript2": "^0.22.0"
"jest": "^24.9.0"
},
"peerDependencies": {
"react": "*",
Expand Down
11 changes: 1 addition & 10 deletions core/specification/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,7 @@
"docz-rollup": "^2.1.0",
"eslint": "^6.5.1",
"eslint-config-docz-ts": "^2.1.0",
"jest": "^24.9.0",
"rollup-plugin-alias": "^1.5.2",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.0.1",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-peer-deps-external": "^2.2.0",
"rollup-plugin-postcss": "^2.0.3",
"rollup-plugin-progress": "^1.1.1",
"rollup-plugin-typescript2": "^0.22.0"
"jest": "^24.9.0"
},
"publishConfig": {
"access": "public"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ docsControlsEditorsTable.story = {
parameters: {
controls: {
name: { type: ControlTypes.TEXT, label: 'Name', value: 'Mark' },
age: { type: ControlTypes.NUMBER, label: 'Age', value: 19 },
age: { type: ControlTypes.NUMBER, label: 'Age', value: 19, min: 10, max: 75 },
clickMe: {
type: ControlTypes.BUTTON,
label: 'button click',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const docsControlsEditorsTable = ({ name, age }: DocsControlsEditorsTable
docsControlsEditorsTable.story = {
parameters: {
controls: {
age: { type: ControlTypes.NUMBER, label: 'Age', value: 19, order: 2 },
age: { type: ControlTypes.NUMBER, label: 'Age', value: 19, min: 10, max: 75, step: 5 },
},
},
};
11 changes: 1 addition & 10 deletions integrations/storybook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,7 @@
"@storybook/csf": "*",
"@storybook/react": "*",
"@storybook/theming": "*",
"react": "*",
"rollup-plugin-alias": "^1.5.2",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.0.1",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-peer-deps-external": "^2.2.0",
"rollup-plugin-postcss": "^2.0.3",
"rollup-plugin-progress": "^1.1.1",
"rollup-plugin-typescript2": "^0.22.0"
"react": "*"
},
"publishConfig": {
"access": "public"
Expand Down

0 comments on commit c42d7e4

Please sign in to comment.