From c42d7e4dc55855014f921261d267157afa1636df Mon Sep 17 00:00:00 2001 From: atanasster Date: Wed, 12 Feb 2020 14:36:19 -0500 Subject: [PATCH] fix: number random generator with step and min/max --- core/core/package.json | 11 +-------- core/core/src/randomizeData.ts | 24 +++++++++++++++---- core/editors/package.json | 11 +-------- core/specification/package.json | 11 +-------- .../controls-editors-table.stories.tsx | 2 +- .../stories/inherit-from-kind.stories.tsx | 2 +- integrations/storybook/package.json | 11 +-------- 7 files changed, 26 insertions(+), 46 deletions(-) diff --git a/core/core/package.json b/core/core/package.json index 5a92ece3a..5a1fbd3bf 100644 --- a/core/core/package.json +++ b/core/core/package.json @@ -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" diff --git a/core/core/src/randomizeData.ts b/core/core/src/randomizeData.ts index 5a6aa0290..cfcdd5a25 100644 --- a/core/core/src/randomizeData.ts +++ b/core/core/src/randomizeData.ts @@ -1,5 +1,6 @@ import { ControlTypes, + ComponentControlNumber, ComponentControlOptions, } from '@component-controls/specification'; import { LoadedComponentControls } from './utils'; @@ -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') { diff --git a/core/editors/package.json b/core/editors/package.json index b0c90da4d..c0ae58e52 100644 --- a/core/editors/package.json +++ b/core/editors/package.json @@ -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": "*", diff --git a/core/specification/package.json b/core/specification/package.json index 324265a4e..e2f129e48 100644 --- a/core/specification/package.json +++ b/core/specification/package.json @@ -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" diff --git a/integrations/storybook/.storybook/stories/controls-editors-table.stories.tsx b/integrations/storybook/.storybook/stories/controls-editors-table.stories.tsx index 25c330e0e..609f58938 100644 --- a/integrations/storybook/.storybook/stories/controls-editors-table.stories.tsx +++ b/integrations/storybook/.storybook/stories/controls-editors-table.stories.tsx @@ -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', diff --git a/integrations/storybook/.storybook/stories/inherit-from-kind.stories.tsx b/integrations/storybook/.storybook/stories/inherit-from-kind.stories.tsx index 6660d5011..50bd1b82e 100644 --- a/integrations/storybook/.storybook/stories/inherit-from-kind.stories.tsx +++ b/integrations/storybook/.storybook/stories/inherit-from-kind.stories.tsx @@ -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 }, }, }, }; diff --git a/integrations/storybook/package.json b/integrations/storybook/package.json index 5d7e702ab..38da44ab8 100644 --- a/integrations/storybook/package.json +++ b/integrations/storybook/package.json @@ -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"