From 6b5da474327dfb63932385a917caedad2a660bff Mon Sep 17 00:00:00 2001 From: fzaninotto Date: Thu, 20 Aug 2020 11:08:40 +0200 Subject: [PATCH] Convert data-generator package to TypeScript Refs #4505 --- .../src/{categories.js => categories.ts} | 0 .../src/{commands.js => commands.ts} | 0 .../src/{customers.js => customers.ts} | 0 .../src/{finalize.js => finalize.ts} | 0 .../data-generator/src/{index.js => index.ts} | 21 ++++++++++++++----- examples/data-generator/src/invoices.js | 18 ---------------- examples/data-generator/src/invoices.ts | 21 +++++++++++++++++++ .../src/{products.js => products.ts} | 0 .../src/{reviews.js => reviews.ts} | 0 .../data-generator/src/{utils.js => utils.ts} | 2 +- 10 files changed, 38 insertions(+), 24 deletions(-) rename examples/data-generator/src/{categories.js => categories.ts} (100%) rename examples/data-generator/src/{commands.js => commands.ts} (100%) rename examples/data-generator/src/{customers.js => customers.ts} (100%) rename examples/data-generator/src/{finalize.js => finalize.ts} (100%) rename examples/data-generator/src/{index.js => index.ts} (53%) delete mode 100644 examples/data-generator/src/invoices.js create mode 100644 examples/data-generator/src/invoices.ts rename examples/data-generator/src/{products.js => products.ts} (100%) rename examples/data-generator/src/{reviews.js => reviews.ts} (100%) rename examples/data-generator/src/{utils.js => utils.ts} (93%) diff --git a/examples/data-generator/src/categories.js b/examples/data-generator/src/categories.ts similarity index 100% rename from examples/data-generator/src/categories.js rename to examples/data-generator/src/categories.ts diff --git a/examples/data-generator/src/commands.js b/examples/data-generator/src/commands.ts similarity index 100% rename from examples/data-generator/src/commands.js rename to examples/data-generator/src/commands.ts diff --git a/examples/data-generator/src/customers.js b/examples/data-generator/src/customers.ts similarity index 100% rename from examples/data-generator/src/customers.js rename to examples/data-generator/src/customers.ts diff --git a/examples/data-generator/src/finalize.js b/examples/data-generator/src/finalize.ts similarity index 100% rename from examples/data-generator/src/finalize.js rename to examples/data-generator/src/finalize.ts diff --git a/examples/data-generator/src/index.js b/examples/data-generator/src/index.ts similarity index 53% rename from examples/data-generator/src/index.js rename to examples/data-generator/src/index.ts index 89e7b26b276..9755e8c22ad 100644 --- a/examples/data-generator/src/index.js +++ b/examples/data-generator/src/index.ts @@ -1,3 +1,5 @@ +import { Record } from 'ra-core'; + import generateCustomers from './customers'; import generateCategories from './categories'; import generateProducts from './products'; @@ -6,13 +8,22 @@ import generateInvoices from './invoices'; import generateReviews from './reviews'; import finalize from './finalize'; -export default (options = { serializeDate: true }) => { - const db = {}; +export interface Db { + customers: Record[]; + categories: Record[]; + products: Record[]; + commands: Record[]; + invoices: Record[]; + reviews: Record[]; +} + +export default (options = { serializeDate: true }): Db => { + const db = {} as Db; db.customers = generateCustomers(db, options); - db.categories = generateCategories(db, options); - db.products = generateProducts(db, options); + db.categories = generateCategories(); + db.products = generateProducts(db); db.commands = generateCommands(db, options); - db.invoices = generateInvoices(db, options); + db.invoices = generateInvoices(db); db.reviews = generateReviews(db, options); finalize(db); diff --git a/examples/data-generator/src/invoices.js b/examples/data-generator/src/invoices.js deleted file mode 100644 index 99c5a5e1a41..00000000000 --- a/examples/data-generator/src/invoices.js +++ /dev/null @@ -1,18 +0,0 @@ -export default db => { - let id = 0; - - return db.commands - .filter(command => command.status !== 'delivered') - .sort((a, b) => new Date(a.date) - new Date(b.date)) - .map(command => ({ - id: id++, - date: command.date, - command_id: command.id, - customer_id: command.customer_id, - total_ex_taxes: command.total_ex_taxes, - delivery_fees: command.delivery_fees, - tax_rate: command.tax_rate, - taxes: command.taxes, - total: command.total, - })); -}; diff --git a/examples/data-generator/src/invoices.ts b/examples/data-generator/src/invoices.ts new file mode 100644 index 00000000000..bdf4d65d95b --- /dev/null +++ b/examples/data-generator/src/invoices.ts @@ -0,0 +1,21 @@ +export default db => { + let id = 0; + + return ( + db.commands + .filter(command => command.status !== 'delivered') + // @ts-ignore + .sort((a, b) => new Date(a.date) - new Date(b.date)) + .map(command => ({ + id: id++, + date: command.date, + command_id: command.id, + customer_id: command.customer_id, + total_ex_taxes: command.total_ex_taxes, + delivery_fees: command.delivery_fees, + tax_rate: command.tax_rate, + taxes: command.taxes, + total: command.total, + })) + ); +}; diff --git a/examples/data-generator/src/products.js b/examples/data-generator/src/products.ts similarity index 100% rename from examples/data-generator/src/products.js rename to examples/data-generator/src/products.ts diff --git a/examples/data-generator/src/reviews.js b/examples/data-generator/src/reviews.ts similarity index 100% rename from examples/data-generator/src/reviews.js rename to examples/data-generator/src/reviews.ts diff --git a/examples/data-generator/src/utils.js b/examples/data-generator/src/utils.ts similarity index 93% rename from examples/data-generator/src/utils.js rename to examples/data-generator/src/utils.ts index d73a688e0a3..b1f5d5f52de 100644 --- a/examples/data-generator/src/utils.js +++ b/examples/data-generator/src/utils.ts @@ -12,7 +12,7 @@ export const weightedArrayElement = (values, weights) => export const weightedBoolean = likelyhood => faker.random.number(99) < likelyhood; -export const randomDate = (minDate, maxDate) => { +export const randomDate = (minDate?: Date, maxDate?: Date) => { const minTs = minDate instanceof Date ? minDate.getTime()