Skip to content

Commit 82ab145

Browse files
Shinigami92damienwebdev
authored andcommitted
feat: migrate commerce (#106)
1 parent 0abec83 commit 82ab145

2 files changed

Lines changed: 153 additions & 1 deletion

File tree

src/commerce.ts

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import type { Faker } from '.';
2+
3+
export class Commerce {
4+
constructor(private readonly faker: Faker) {
5+
// Bind `this` so namespaced is working correctly
6+
for (const name of Object.getOwnPropertyNames(Commerce.prototype)) {
7+
if (name === 'constructor' || typeof this[name] !== 'function') {
8+
continue;
9+
}
10+
this[name] = this[name].bind(this);
11+
}
12+
}
13+
14+
/**
15+
* color
16+
*
17+
* @method faker.commerce.color
18+
*/
19+
color(): string {
20+
return this.faker.random.arrayElement(
21+
this.faker.definitions.commerce.color
22+
);
23+
}
24+
25+
/**
26+
* department
27+
*
28+
* @method faker.commerce.department
29+
*/
30+
department(): string {
31+
return this.faker.random.arrayElement(
32+
this.faker.definitions.commerce.department
33+
);
34+
}
35+
36+
/**
37+
* productName
38+
*
39+
* @method faker.commerce.productName
40+
*/
41+
productName(): string {
42+
return (
43+
this.faker.commerce.productAdjective() +
44+
' ' +
45+
this.faker.commerce.productMaterial() +
46+
' ' +
47+
this.faker.commerce.product()
48+
);
49+
}
50+
51+
/**
52+
* price
53+
*
54+
* @method faker.commerce.price
55+
* @param min
56+
* @param max
57+
* @param dec
58+
* @param symbol
59+
*/
60+
price(
61+
min: number = 1,
62+
max: number = 1000,
63+
dec: number = 2,
64+
symbol: string = ''
65+
): string {
66+
if (min < 0 || max < 0) {
67+
return symbol + 0.0;
68+
}
69+
70+
const randValue = this.faker.datatype.number({ max: max, min: min });
71+
72+
return (
73+
symbol +
74+
(Math.round(randValue * Math.pow(10, dec)) / Math.pow(10, dec)).toFixed(
75+
dec
76+
)
77+
);
78+
}
79+
80+
// TODO @Shinigami92 2022-01-12: unimplemented member functions
81+
82+
/*
83+
categories (num) {
84+
var categories = [];
85+
86+
do {
87+
var category = this.faker.random.arrayElement(this.faker.definitions.commerce.department);
88+
if(categories.indexOf(category) === -1) {
89+
categories.push(category);
90+
}
91+
} while(categories.length < num);
92+
93+
return categories;
94+
};
95+
96+
*/
97+
/*
98+
mergeCategories (categories) {
99+
var separator = this.faker.definitions.separator || " &";
100+
// TODO: find undefined here
101+
categories = categories || this.faker.definitions.commerce.categories;
102+
var commaSeparated = categories.slice(0, -1).join(', ');
103+
104+
return [commaSeparated, categories[categories.length - 1]].join(separator + " ");
105+
};
106+
*/
107+
108+
/**
109+
* productAdjective
110+
*
111+
* @method faker.commerce.productAdjective
112+
*/
113+
productAdjective(): string {
114+
return this.faker.random.arrayElement(
115+
this.faker.definitions.commerce.product_name.adjective
116+
);
117+
}
118+
119+
/**
120+
* productMaterial
121+
*
122+
* @method faker.commerce.productMaterial
123+
*/
124+
productMaterial(): string {
125+
return this.faker.random.arrayElement(
126+
this.faker.definitions.commerce.product_name.material
127+
);
128+
}
129+
130+
/**
131+
* product
132+
*
133+
* @method faker.commerce.product
134+
*/
135+
product(): string {
136+
return this.faker.random.arrayElement(
137+
this.faker.definitions.commerce.product_name.product
138+
);
139+
}
140+
141+
/**
142+
* productDescription
143+
*
144+
* @method faker.commerce.productDescription
145+
*/
146+
productDescription(): string {
147+
return this.faker.random.arrayElement(
148+
this.faker.definitions.commerce.product_description
149+
);
150+
}
151+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Animal } from './animal';
2+
import { Commerce } from './commerce';
23
import { Database } from './database';
34
import { Datatype } from './datatype';
45
import { _Date } from './date';
@@ -176,7 +177,7 @@ export class Faker {
176177

177178
readonly address = new (require('./address'))(this);
178179
readonly animal: Animal = new Animal(this);
179-
readonly commerce = new (require('./commerce'))(this);
180+
readonly commerce: Commerce = new Commerce(this);
180181
readonly company = new (require('./company'))(this);
181182
readonly database: Database = new Database(this);
182183
readonly date: _Date = new _Date(this);

0 commit comments

Comments
 (0)