Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
Browse files Browse the repository at this point in the history
  • Loading branch information
theryansmee committed Mar 22, 2022
2 parents 6160c3c + 21bcefb commit 485782e
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 8 deletions.
3 changes: 3 additions & 0 deletions docs/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module.exports = {
favicon: 'img/favicon.ico',
organizationName: 'ngneat',
projectName: 'falso',
clientModules: [
require.resolve('./src/consolePlayground/index.js'),
],
themeConfig: {
colorMode: {
respectPrefersColorScheme: true,
Expand Down
12 changes: 12 additions & 0 deletions docs/src/consolePlayground/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as falso from '../theme/ReactLiveScope/falso.min';

if (typeof window !== 'undefined') {
window.onload = function () {
const titleStyles = 'color:#8a76d9; font-size: 22px; font-weight:bold;';
const copyStyles = 'color:white; font-size: 14px';
const exampleStyles = 'color:#8a76d9; font-size: 14px;';
console.log('%cFalso\n\n' + '%cYou can use _ to access Falso functions here in the console. \n\n%c' + 'For example: _.randFullName()', titleStyles, copyStyles, exampleStyles)

window._ = falso;
};
}
6 changes: 6 additions & 0 deletions packages/falso/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).

# [3.1.0](https://github.com/ngneat/falso/compare/falso-3.0.0...falso-3.1.0) (2022-03-22)

### Features

- add precision to randNumber ([44fdcd6](https://github.com/ngneat/falso/commit/44fdcd61014705de6aa8368f2276ebd6fd80abd0)), closes [#226](https://github.com/ngneat/falso/issues/226)

# [3.0.0](https://github.com/ngneat/falso/compare/falso-2.27.0...falso-3.0.0) (2022-03-08)

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion packages/falso/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ngneat/falso",
"version": "3.0.0",
"version": "3.1.0",
"license": "MIT",
"description": "All the Fake Data for All Your Real Needs",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion packages/falso/src/lib/credit-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface CreditCard {
/**
* Generate a random credit card.
*
* @category finance, entity
* @category finance, entities
*
* @example
*
Expand Down
14 changes: 13 additions & 1 deletion packages/falso/src/lib/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,22 @@ export interface RandomFloatOptions extends RandomInRangeOptions, FakeOptions {}
*
* randFloat({ length: 10 })
*
* @example
*
* randFloat({ min: 10, max: 20, fraction: 1 }) // 12.5
*
* @example
*
* randFloat({ min: 10, max: 20, fraction: 2 }) // 12.52
*/
export function randFloat<Options extends RandomFloatOptions = never>(
options?: Options
) {
const o = { ...options, fraction: options?.fraction ?? 2 };
const o: RandomFloatOptions = {
...options,
fraction: options?.fraction ?? 2,
};
return fake(() => getRandomInRange(o), options);
}

randFloat();
27 changes: 22 additions & 5 deletions packages/falso/src/lib/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import {
RandomInRangeOptions,
} from './core/core';

export interface RandomNumberOptions extends FakeOptions {
min?: number;
max?: number;
export interface RandomNumberOptions extends RandomInRangeOptions, FakeOptions {
precision?: number;
}

/**
Expand All @@ -27,14 +26,32 @@ export interface RandomNumberOptions extends FakeOptions {
*
* randNumber({ min: 10, max: 1000 }) // default is 'min': 0, 'max': 999_999
*
* @example
*
* randNumber({ min: 1000, max: 100000, precision: 1000 }) // 67000
*
* @example
*
* randNumber({ min: 1000, max: 2000, precision: 100 }) // 1200
*
* @example
*
* randNumber({ min: 1000, max: 2000, precision: 10 }) // 1250
*/
export function randNumber<Options extends RandomNumberOptions = never>(
options?: Options
) {
const config: RandomInRangeOptions = {
const o: RandomNumberOptions = {
min: options?.min || 0,
max: options?.max || 999_999,
precision: options?.precision,
};

return fake(() => getRandomInRange(config), options);
return fake(() => {
const num = getRandomInRange(o);
if (o.precision !== undefined) {
return Math.floor(num / o.precision) * o.precision;
}
return num;
}, options);
}
17 changes: 17 additions & 0 deletions packages/falso/src/tests/number.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { randNumber } from '../lib/number';

describe('randNumber', () => {
it('should return a random number', () => {
const [min, max] = [1, 999_999];
const num = randNumber({ min, max });
expect(typeof num).toBe('number');
expect(num).toBeGreaterThanOrEqual(min);
expect(num).toBeLessThanOrEqual(max);
});

it('should support precision', () => {
const num = randNumber({ min: 1000, max: 2000, precision: 100 });
expect(typeof num).toBe('number');
expect(String(num)).toMatch(/^\d\d00$/);
});
});
6 changes: 6 additions & 0 deletions packages/falso/src/tests/random-float.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ describe('random-float', () => {
expect(randFloat({ max })).toBeLessThanOrEqual(max);
expect(randFloat({ max })).toBeGreaterThanOrEqual(1.0);
});

it('should support fraction', () => {
const num = randFloat({ min: 100, max: 200, fraction: 2 });
expect(typeof num).toBe('number');
expect(String(num)).toMatch(/^\d+\.\d\d$/);
});
});

0 comments on commit 485782e

Please sign in to comment.