Skip to content

Commit

Permalink
Improve build system, documentation and types
Browse files Browse the repository at this point in the history
  • Loading branch information
PunGy committed May 3, 2022
1 parent 804304e commit 91306dc
Show file tree
Hide file tree
Showing 30 changed files with 818 additions and 172 deletions.
22 changes: 16 additions & 6 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
name: Test and send report
name: Test
on: [push]

jobs:
run:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest]
node: [ '12', '14', '15' ]
node-versions:
- 14

steps:
- uses: actions/checkout@v2
- name: Setup Node
uses: actions/setup-node@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- uses: actions/cache@v2
id: cache
with:
path: node_modules/
key: ${{ runner.os }}-node${{ matrix.node-version }}-${{ hashFiles('yarn*.lock') }}

- name: Install dependencies
run: yarn
- name: Generate coverage report
run: yarn test:coverage
- name: Upload coverage to Codecov
Expand Down
38 changes: 28 additions & 10 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
name: Linting
name: Linting and Testing
on: [push, pull_request]

jobs:
check:
runs-on: ubuntu-latest
run:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest]
node-versions:
- 10
- 12
- 14

steps:
- uses: actions/checkout@v2
- name: Setup Node
uses: actions/setup-node@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: '14.x'
- name: Install dependencies
run: yarn
- name: Lint
run: yarn lint
node-version: ${{ matrix.node-version }}

- uses: actions/cache@v2
id: cache
with:
path: node_modules/
key: ${{ runner.os }}-node${{ matrix.node-version }}-${{ hashFiles('yarn*.lock') }}

- run: yarn ci
if: steps.cache.outputs.cache-hit != 'true'

- run: yarn test:ci
- run: yarn lint
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release

on:
push:
tags:
- v1.*

jobs:
release:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- run : yarn ci
- run : yarn build:docs
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs/build
keep_files: true
- name: Publish to npm
run : |
npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
npm publish --ignore-scripts
env :
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,7 @@ types
.tern-port

# Mac local files
.DS_Store
.DS_Store

# Documentation page
docs
68 changes: 52 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,45 @@

`mockaroni` is a tool for fast generating mock javascript entities.

### Usage
# Usage

Input:
```ts
import { replicate, str, num, oneOf, date, listOf } from './mockaroni'
import { replicate, str, num, oneOf, date, listOf, text } from 'mockaroni'

interface Worker
{
name: string;
age: number;
position: 'manager' | 'director' | 'clerk';
dateOfEmployment: Date;
achievements?: Array<'owl' | 'hard worker' | 'coffee lower' | 'gambler'>;
achievements: Array<'owl' | 'hard worker' | 'coffee lower' | 'gambler'> | null;
place: {
officeId: string;
floor: number;
};
biography: string;
}

// Spawn 100 workers schema
const workers: Array<Worker> = replicate({
size: 100,
schema: () => ({
// Spawn capitalized string of chars with size in range of 4 to 8
name: str({ format: 'capitalized', type: 'alpha', size: 1 })
+ str({ format: 'normal', type: 'alpha', size: num({ min: 4, max: 8 }) }),
name: text({ size: 1, type: 'names' }),

// Spawn number from 15 to 90
age: num({ min: 15, max: 90 }),

// Select on of the options provided in list
position: oneOf({ list: ['manager', 'director', 'clerk'] }),
position: oneOf({ list: ['manager', 'director', 'clerk'] as const }),

// Spawn date in selected edges
dateOfEmployment: date({ min: new Date('2018'), max: new Date('2020') }),

// Spawn list of achievements. Count of achievements can be from 1 to 4, or null
achievements: listOf({
list: ['owl', 'hard worker', 'coffee lower', 'gambler'],
list: ['owl', 'hard worker', 'coffee lower', 'gambler'] as const,
size: num({ min: 1, max: 4 }),
nullable: true,
}),
Expand All @@ -55,6 +55,9 @@ const workers: Array<Worker> = replicate({
// Spawn number from 1 to 20
floor: num({ min: 1, max: 20 }),
},

// Generate text with 10-30 words
biography: text({ size: num({ min: 10, max: 30 }) }),
}),
})

Expand All @@ -65,21 +68,54 @@ output:
```js
[
{
name: 'Yphnad',
age: 27,
name: 'Annie',
age: 74,
position: 'manager',
dateOfEmployment: 2019-10-24T11:20:41.367Z,
achievements: [ 'hard worker', 'hard worker', 'hard worker' ],
place: { officeId: 'eg2wqrm3q5', floor: 17 },
biography: 'muddle abandoned clear stupid obtainable earn arm lumber nostalgic race toothsome reflective offer identify tow woozy butter parcel smile dapper tow shivering'
},
{
name: 'Lachlan',
age: 74,
position: 'clerk',
dateOfEmployment: 2018-10-13T15:00:00.712Z,
dateOfEmployment: 2018-07-01T14:04:21.348Z,
achievements: null,
place: { officeId: '6fm4g5049d', floor: 6 }
place: { officeId: '92b687038k', floor: 7 },
biography: 'raise frightened word learned shame hellish sleep sheep direful cable absurd'
},
{
name: 'Yztxrypdo',
age: 28,
name: 'Hamzah',
age: 49,
position: 'director',
dateOfEmployment: 2019-07-17T17:25:41.517Z,
achievements: [ 'owl', 'coffee lower' ],
place: { officeId: 'rbs10v5d4j', floor: 7 }
dateOfEmployment: 2019-01-24T22:33:43.132Z,
achievements: null,
place: { officeId: 'jfscywmz4n', floor: 19 },
biography: 'parcel magic craven identify frightened mint toothsome zippy protect huge shivering sea clever rat actually filthy kittens'
},
...
]
```

# Install

Library can be install via packet manager like npm or yarn
```shell
yarn add mockaroni
```
or
```shell
npm install mockaroni
```

Then, you can import it like commonjs or es6 import
```js
import { num, text } from 'mockaroni'
// or
const { num, text } = require('mockaroni')
```

### Browser

You also can download
51 changes: 51 additions & 0 deletions examples/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { replicate, str, num, oneOf, date, listOf, text } from '../src/index'

interface Worker
{
name: string;
age: number;
position: 'manager' | 'director' | 'clerk';
dateOfEmployment: Date;
achievements: Array<'owl' | 'hard worker' | 'coffee lower' | 'gambler'> | null;
place: {
officeId: string;
floor: number;
};
biography: string;
}

// Spawn 100 workers schema
const workers: Array<Worker> = replicate({
size: 100,
schema: () => ({
// Spawn capitalized string of chars with size in range of 4 to 8
name: text({ size: 1, type: 'names' }),

// Spawn number from 15 to 90
age: num({ min: 15, max: 90 }),

// Select on of the options provided in list
position: oneOf({ list: ['manager', 'director', 'clerk'] as const }),

// Spawn date in selected edges
dateOfEmployment: date({ min: new Date('2018'), max: new Date('2020') }),

// Spawn list of achievements. Count of achievements can be from 1 to 4, or null
achievements: listOf({
list: ['owl', 'hard worker', 'coffee lower', 'gambler'] as const,
size: num({ min: 1, max: 4 }),
nullable: true,
}),

place: {
// Spawn 10 length alphanumeric string
officeId: str({ type: 'alphanumeric', size: 10 }),
// Spawn number from 1 to 20
floor: num({ min: 1, max: 20 }),
},

biography: text({ size: num({ min: 10, max: 30 }) }),
}),
})

console.log(workers)
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,22 @@
"LICENSE"
],
"scripts": {
"ci": "yarn clean && yarn install --frozen-lockfile",
"clean": "rm -rf node_modules",
"example": "ts-node --project tsconfig.json examples/worker.ts",
"test": "jest --maxWorkers=50%",
"test:ci": "jest --runInBand",
"test:coverage": "jest --coverage",
"test:dev": "jest --watch --maxWorkers=25%",
"lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
"type-check": "tsc --project tsconfig.production.json --noEmit",
"type-check": "tsc --project tsconfig.json",
"build": "yarn run build:code && yarn run build:types",
"build:code": "webpack-cli",
"build:code": "tsc --project tsconfig.build.json",
"build:web": "yarn run build:web:common && yarn run build:web:module",
"build:web:common": "NODE_ENV=common webpack-cli --config webpack.config.js",
"build:web:module": "NODE_ENV=module webpack-cli --config webpack.config.js",
"build:types": "tsc --project tsconfig.typegen.json && rm -rf ./temp",
"build:docs": "typedoc ./src/index.ts",
"postinstall": "husky install",
"prepublishOnly": "pinst --disable",
"postpublish": "pinst --enable"
Expand All @@ -53,6 +60,7 @@
"prettier": "^2.2.1",
"ts-jest": "^26.5.1",
"ts-loader": "^9.3.0",
"ts-node": "^10.7.0",
"typedoc": "^0.22.15",
"typescript": "^4.1.5",
"webpack": "^5.72.0",
Expand Down
2 changes: 1 addition & 1 deletion src/globalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type MockaroniConfig = {
*/
random: () => number;
}
export const globalConfig = {
export const globalConfig: MockaroniConfig = {
random: Math.random,
}

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './primitives/oneOf'
export * from './primitives/num'
export * from './primitives/str'
export * from './primitives/date'
export * from './primitives/text'

export * from './lists/range'
export * from './lists/replicate'
Expand Down
27 changes: 20 additions & 7 deletions src/lists/listOf.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import { Config } from '../utils'
import { ArrayElementsType, Nullable, nullable } from '../utils'
import { oneOf } from '../primitives/oneOf'
import { trueOrFalse } from '../primitives/boolean'

export type ListOfReturnValue<T> = Array<ArrayElementsType<ArrayLike<T>>>;

export type ListOfConfig<T> = {
/**
* The size of resulting array
*/
size: number;

/**
* List of values which resulting array will contain
*/
list: ArrayLike<T>;
}
export function listOf<T>(config: ListOfConfig<T>): ListOfReturnValue<T>
export function listOf<T>(config: Nullable<ListOfConfig<T>, false>): ListOfReturnValue<T>
export function listOf<T>(config: Nullable<ListOfConfig<T>>): ListOfReturnValue<T> | null

/**
* Generates an array of provided values from list param
* @param config {Object}
* @param config.size - the size of resulting array
* @param config.list - list of values which resulting array will contain
* @param config.nullable - result can be null
*/
export const listOf = <T>(config: Config<{ size: number; list: Array<T>; }>): Array<T> =>
export function listOf<T>(config: ListOfConfig<T> | Nullable<ListOfConfig<T>>): ListOfReturnValue<T> | null
{
if (config.nullable) if (trueOrFalse()) return null
if (nullable(config)) if (trueOrFalse()) return null

return Array.from(
Array(config.size),
Expand Down
Loading

0 comments on commit 91306dc

Please sign in to comment.