Skip to content

Commit

Permalink
Convert codebase to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan authored May 1, 2020
2 parents d42e1c2 + c63c673 commit 5b6ecfb
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-flow"
"@babel/preset-typescript"
]
}
9 changes: 9 additions & 0 deletions .d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface Model extends Object {}

interface ModelDefinition extends Object {
[model: string]: Function
}

interface Overrides extends Object {
[attribute: string]: any
}
5 changes: 2 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"parser": "babel-eslint",
"parser": "@typescript-eslint/parser",
"extends": [
"plugin:flowtype/recommended",
"plugin:jest/recommended",
"standard"
],
"plugins": [
"flowtype",
"@typescript-eslint",
"jest"
]
}
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
language: node_js
node_js:
- '8'
- '6'
node_js: [10, 12, 13, 14]
2 changes: 1 addition & 1 deletion dist/factoria.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion flow/def.js

This file was deleted.

20 changes: 11 additions & 9 deletions index.js → index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
// @flow
const faker: Faker = require('faker')
/// <reference path=".d.ts"/>
import faker from 'faker'

const definitions: Object = {}
const definitions: ModelDefinition = {}

const factory = (model: string, count: number | Object = 1, overrides: Object = {}) => {
const factory = (model: string, count: number | Overrides = 1, overrides: Overrides = {}): Model | Model[] => {
if (!(model in definitions)) {
throw new Error(`Model \`${model}\` not found.`)
}

if (typeof count === 'object') {
if (typeof count !== 'number') {
return factory(model, 1, count)
}

const resolveOverrides = (): Object => {
const props = Object.assign({}, overrides)

for (let prop in props) {
props[prop] = typeof props[prop] === 'function' ? props[prop].call(this, faker) : props[prop]
props[prop] = props[prop] instanceof Function ? props[prop].call(this, faker) : props[prop]
}

return props
}

const generateModel = (): Object => {
const generateModel = (): Model => {
return Object.assign(definitions[model](faker), resolveOverrides())
}

Expand All @@ -30,15 +32,15 @@ const factory = (model: string, count: number | Object = 1, overrides: Object =

return [...(function * () {
let i = 0
// $FlowFixMe

while (i < count) {
yield generateModel()
++i
}
})()]
}

factory.define = (model: string, attributes: Faker => Object): factory => {
factory.define = (model: string, attributes: (faker: Faker.FakerStatic) => Object) => {
definitions[model] = attributes
return factory
}
Expand Down
44 changes: 23 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
},
"scripts": {
"lint": "eslint .",
"flow": "flow",
"test": "yarn lint && yarn flow && jest --verbose",
"type-check": "tsc --noEmit",
"type-check:watch": "npm run type-check -- --watch",
"test": "yarn lint && jest --verbose",
"build": "rollup -c"
},
"files": [
"index.js"
],
"jest": {
"setupFilesAfterEnv": [
"<rootDir>/test/setup.js"
"<rootDir>/test/setup.ts"
]
},
"main": "dist/factoria.min.js",
Expand All @@ -34,31 +35,32 @@
"dummy",
"schema"
],
"dependencies": {
"@babel/polyfill": "^7.4.4"
},
"peerDepedencies": {
"faker": "^4.1.0"
},
"devDependencies": {
"@babel/cli": "7.8.4",
"@babel/core": "7.8.4",
"@babel/preset-env": "7.8.4",
"@babel/preset-flow": "7.8.3",
"babel-eslint": "8.2.6",
"eslint": "4.19.1",
"eslint-config-standard": "11.0.0",
"eslint-plugin-flowtype": "2.50.3",
"eslint-plugin-import": "2.20.1",
"eslint-plugin-jest": "21.27.2",
"eslint-plugin-node": "6.0.1",
"eslint-plugin-promise": "3.8.0",
"eslint-plugin-standard": "3.1.0",
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@babel/preset-typescript": "^7.9.0",
"@rollup/plugin-typescript": "^4.1.1",
"@types/faker": "^4.1.11",
"@types/jest": "^25.2.1",
"@typescript-eslint/eslint-plugin": "^2.30.0",
"@typescript-eslint/parser": "^2.30.0",
"eslint": "^6.8.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jest": "^23.8.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"faker": "4.1.0",
"flow-bin": "0.66.0",
"jest": "24.9.0",
"rollup": "1.31.1",
"rollup-plugin-babel": "4.3.3",
"rollup-plugin-uglify": "6.0.4"
"rollup-plugin-uglify": "6.0.4",
"tslib": "^1.11.1",
"typescript": "^3.8.3"
}
}
5 changes: 0 additions & 5 deletions renovate.json

This file was deleted.

6 changes: 5 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { uglify } from 'rollup-plugin-uglify'
import typescript from '@rollup/plugin-typescript'
import babel from 'rollup-plugin-babel'

export default {
input: 'index.js',
input: 'index.ts',
context: 'this',
external: ['faker'],
output: {
file: 'dist/factoria.min.js',
format: 'cjs'
},
plugins: [
typescript(),
uglify(),
babel({
exclude: 'node_modules/**'
Expand Down
35 changes: 0 additions & 35 deletions test/index.spec.js

This file was deleted.

38 changes: 38 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import factory from '..'

const keys = <O extends Object>(obj: O): Array<keyof O> => {
return Object.keys(obj) as Array<keyof O>
}

const validate = (user: Model, overrides = {}): void => {
['id', 'email', 'name'].forEach(key => expect(Object.prototype.hasOwnProperty.call(user, key)).toBe(true))
keys(overrides).forEach(key => expect(user[key]).toBe(overrides[key]))
}

describe('factoria', (): void => {
it('generates a model with name', (): void => validate(factory('user')))

it("overrides a model's properties", () => {
validate(factory('user', { email: 'foo@bar.net' }), { email: 'foo@bar.net' })
})

it('generates several models', (): void => {
const users = factory('user', 2) as Model[]
expect(users).toHaveLength(2)
users.forEach(validate)
})

it("overrides multiple models' properties", (): void => {
const users = factory('user', 3, { email: 'foo@bar.net' }) as Model[]
expect(users).toHaveLength(3)
users.forEach(user => validate(user, { email: 'foo@bar.net' }))
})

it('supports define chaining', (): void => {
expect(factory.define('foo', faker => ({}))).toEqual(factory)
})

it('supports functions as property overrides', (): void => {
validate(factory('user', { email: () => 'foo@bar.net' }), { email: 'foo@bar.net' })
})
})
2 changes: 1 addition & 1 deletion test/setup.js → test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import factory from '..'

factory.define('user', faker => ({
factory.define('user', (faker: Faker.FakerStatic): Object => ({
id: faker.random.number(),
name: faker.name.findName(),
email: faker.internet.email()
Expand Down
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "es5",
"strict": true,
"module": "es2015",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"noImplicitThis": false,
"downlevelIteration": true
},
"include": [
"index.ts",
"test/**/*.ts"
]
}

0 comments on commit 5b6ecfb

Please sign in to comment.