Skip to content

Commit 2342792

Browse files
authored
Merge branch 'next' into chore/es-mx-names
2 parents 9ebeb53 + f0e859b commit 2342792

File tree

11 files changed

+408
-30
lines changed

11 files changed

+408
-30
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Faker - Copyright (c) 2022
1+
Faker - Copyright (c) 2022-2023
22

33
This software consists of voluntary contributions made by many individuals.
44
For exact contribution history, see the revision history

src/internal/mersenne/twister.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2022 Faker
2+
* Copyright (c) 2022-2023 Faker
33
*
44
* This is a version of the original source code migrated to TypeScript and
55
* modified by the Faker team.

src/locales/en/company/bs_noun.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export default [
22
'synergies',
3-
'web-readiness',
43
'paradigms',
54
'markets',
65
'partnerships',
@@ -12,14 +11,11 @@ export default [
1211
'communities',
1312
'ROI',
1413
'solutions',
15-
'e-tailers',
16-
'e-services',
1714
'action-items',
1815
'portals',
1916
'niches',
2017
'technologies',
2118
'content',
22-
'vortals',
2319
'supply-chains',
2420
'convergence',
2521
'relationships',
@@ -29,7 +25,6 @@ export default [
2925
'e-commerce',
3026
'systems',
3127
'bandwidth',
32-
'infomediaries',
3328
'models',
3429
'mindshare',
3530
'deliverables',
@@ -44,4 +39,5 @@ export default [
4439
'web services',
4540
'methodologies',
4641
'blockchains',
42+
'lifetime value',
4743
];

src/modules/helpers/index.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,55 @@ export class HelpersModule {
459459
return array[index];
460460
}
461461

462+
/**
463+
* Returns a weighted random element from the given array. Each element of the array should be an object with two keys `weight` and `value`.
464+
*
465+
* - Each `weight` key should be a number representing the probability of selecting the value, relative to the sum of the weights. Weights can be any positive float or integer.
466+
* - Each `value` key should be the corresponding value.
467+
*
468+
* For example, if there are two values A and B, with weights 1 and 2 respectively, then the probability of picking A is 1/3 and the probability of picking B is 2/3.
469+
*
470+
* @template T The type of the entries to pick from.
471+
* @param array Array to pick the value from.
472+
*
473+
* @example
474+
* faker.helpers.weightedArrayElement([{ weight: 5, value: 'sunny' }, { weight: 4, value: 'rainy' }, { weight: 1, value: 'snowy' }]) // 'sunny', 50% of the time, 'rainy' 40% of the time, 'snowy' 10% of the time
475+
*
476+
* @since 8.0.0
477+
*/
478+
weightedArrayElement<T>(
479+
array: ReadonlyArray<{ weight: number; value: T }>
480+
): T {
481+
if (array.length === 0) {
482+
throw new FakerError(
483+
'weightedArrayElement expects an array with at least one element'
484+
);
485+
}
486+
487+
if (!array.every((elt) => elt.weight > 0)) {
488+
throw new FakerError(
489+
'weightedArrayElement expects an array of { weight, value } objects where weight is a positive number'
490+
);
491+
}
492+
493+
const total = array.reduce((acc, { weight }) => acc + weight, 0);
494+
const random = this.faker.number.float({
495+
min: 0,
496+
max: total,
497+
precision: 1e-9,
498+
});
499+
let current = 0;
500+
for (const { weight, value } of array) {
501+
current += weight;
502+
if (random < current) {
503+
return value;
504+
}
505+
}
506+
507+
// In case of rounding errors, return the last element
508+
return array[array.length - 1].value;
509+
}
510+
462511
/**
463512
* Returns a subset with random elements of the given array in random order.
464513
*

src/modules/internet/user-agent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2022 Faker
2+
* Copyright (c) 2022-2023 Faker
33
*
44
* This is a version of the original code migrated to TypeScript and modified
55
* by the Faker team.

src/modules/location/index.ts

Lines changed: 95 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '../..';
2+
import { deprecated } from '../../internal/deprecated';
23

34
/**
45
* Module to generate addresses and locations.
@@ -368,6 +369,27 @@ export class LocationModule {
368369
);
369370
}
370371

372+
/**
373+
* Generates a random GPS coordinate within the specified radius from the given coordinate.
374+
*
375+
* @param options The options for generating a GPS coordinate.
376+
* @param options.origin The original coordinate to get a new coordinate close to.
377+
* If no coordinate is given, a random one will be chosen.
378+
* @param options.radius The maximum distance from the given coordinate to the new coordinate. Defaults to `10`.
379+
* @param options.isMetric If `true` assume the radius to be in kilometers. If `false` for miles. Defaults to `false`.
380+
*
381+
* @example
382+
* faker.location.nearbyGPSCoordinate() // [ 33.8475, -170.5953 ]
383+
* faker.location.nearbyGPSCoordinate({ origin: [33, -170] }) // [ 33.0165, -170.0636 ]
384+
* faker.location.nearbyGPSCoordinate({ origin: [33, -170], radius: 1000, isMetric: true }) // [ 37.9163, -179.2408 ]
385+
*
386+
* @since 8.0.0
387+
*/
388+
nearbyGPSCoordinate(options?: {
389+
origin?: [latitude: number, longitude: number];
390+
radius?: number;
391+
isMetric?: boolean;
392+
}): [latitude: number, longitude: number];
371393
/**
372394
* Generates a random GPS coordinate within the specified radius from the given coordinate.
373395
*
@@ -382,14 +404,74 @@ export class LocationModule {
382404
* faker.location.nearbyGPSCoordinate([33, -170], 1000, true) // [ 37.9163, -179.2408 ]
383405
*
384406
* @since 8.0.0
407+
*
408+
* @deprecated Use `faker.location.nearbyGPSCoordinate({ origin, radius, isMetric })` instead.
385409
*/
386410
nearbyGPSCoordinate(
387411
coordinate?: [latitude: number, longitude: number],
388-
radius: number = 10,
389-
isMetric: boolean = false
412+
radius?: number,
413+
isMetric?: boolean
414+
): [latitude: number, longitude: number];
415+
/**
416+
* Generates a random GPS coordinate within the specified radius from the given coordinate.
417+
*
418+
* @param options The options for generating a GPS coordinate.
419+
* @param options.origin The original coordinate to get a new coordinate close to.
420+
* If no coordinate is given, a random one will be chosen.
421+
* @param options.radius The maximum distance from the given coordinate to the new coordinate. Defaults to `10`.
422+
* @param options.isMetric If `true` assume the radius to be in kilometers. If `false` for miles. Defaults to `false`.
423+
* @param legacyRadius Deprecated, use `options.radius` instead.
424+
* @param legacyIsMetric Deprecated, use `options.isMetric` instead.
425+
*
426+
* @example
427+
* faker.location.nearbyGPSCoordinate() // [ 33.8475, -170.5953 ]
428+
* faker.location.nearbyGPSCoordinate({ origin: [33, -170] }) // [ 33.0165, -170.0636 ]
429+
* faker.location.nearbyGPSCoordinate({ origin: [33, -170], radius: 1000, isMetric: true }) // [ 37.9163, -179.2408 ]
430+
*
431+
* @since 8.0.0
432+
*/
433+
nearbyGPSCoordinate(
434+
options?:
435+
| [latitude: number, longitude: number]
436+
| {
437+
origin?: [latitude: number, longitude: number];
438+
radius?: number;
439+
isMetric?: boolean;
440+
},
441+
legacyRadius?: number,
442+
legacyIsMetric?: boolean
443+
): [latitude: number, longitude: number];
444+
nearbyGPSCoordinate(
445+
options:
446+
| [latitude: number, longitude: number]
447+
| {
448+
origin?: [latitude: number, longitude: number];
449+
radius?: number;
450+
isMetric?: boolean;
451+
} = {},
452+
legacyRadius: number = 10,
453+
legacyIsMetric: boolean = false
390454
): [latitude: number, longitude: number] {
391-
// If there is no coordinate, the best we can do is return a random GPS coordinate.
392-
if (coordinate === undefined) {
455+
if (Array.isArray(options)) {
456+
deprecated({
457+
deprecated:
458+
'faker.location.nearbyGPSCoordinate(coordinate, radius, isMetric)',
459+
proposed:
460+
'faker.location.nearbyGPSCoordinate({ origin, radius, isMetric })',
461+
since: '8.0',
462+
until: '9.0',
463+
});
464+
options = { origin: options };
465+
}
466+
467+
const {
468+
origin,
469+
radius = legacyRadius,
470+
isMetric = legacyIsMetric,
471+
} = options;
472+
473+
// If there is no origin, the best we can do is return a random GPS coordinate.
474+
if (origin == null) {
393475
return [this.latitude(), this.longitude()];
394476
}
395477

@@ -414,22 +496,22 @@ export class LocationModule {
414496

415497
const distanceInDegree = distanceInKm / kmPerDegree; // in °
416498

417-
const newCoordinate: [latitude: number, longitude: number] = [
418-
coordinate[0] + Math.sin(angleRadians) * distanceInDegree,
419-
coordinate[1] + Math.cos(angleRadians) * distanceInDegree,
499+
const coordinate: [latitude: number, longitude: number] = [
500+
origin[0] + Math.sin(angleRadians) * distanceInDegree,
501+
origin[1] + Math.cos(angleRadians) * distanceInDegree,
420502
];
421503

422504
// Box latitude [-90°, 90°]
423-
newCoordinate[0] = newCoordinate[0] % 180;
424-
if (newCoordinate[0] < -90 || newCoordinate[0] > 90) {
425-
newCoordinate[0] = Math.sign(newCoordinate[0]) * 180 - newCoordinate[0];
426-
newCoordinate[1] += 180;
505+
coordinate[0] = coordinate[0] % 180;
506+
if (coordinate[0] < -90 || coordinate[0] > 90) {
507+
coordinate[0] = Math.sign(coordinate[0]) * 180 - coordinate[0];
508+
coordinate[1] += 180;
427509
}
428510

429511
// Box longitude [-180°, 180°]
430-
newCoordinate[1] = (((newCoordinate[1] % 360) + 540) % 360) - 180;
512+
coordinate[1] = (((coordinate[1] % 360) + 540) % 360) - 180;
431513

432-
return [newCoordinate[0], newCoordinate[1]];
514+
return [coordinate[0], coordinate[1]];
433515
}
434516

435517
/**

test/__snapshots__/company.spec.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Vitest Snapshot v1
22

3-
exports[`company > 42 > bs 1`] = `"seize impactful web services"`;
3+
exports[`company > 42 > bs 1`] = `"seize impactful methodologies"`;
44

55
exports[`company > 42 > bsAdjective 1`] = `"dynamic"`;
66

77
exports[`company > 42 > bsBuzz 1`] = `"seize"`;
88

9-
exports[`company > 42 > bsNoun 1`] = `"portals"`;
9+
exports[`company > 42 > bsNoun 1`] = `"technologies"`;
1010

1111
exports[`company > 42 > catchPhrase 1`] = `"Implemented responsive throughput"`;
1212

@@ -29,13 +29,13 @@ exports[`company > 42 > suffixes 1`] = `
2929
]
3030
`;
3131

32-
exports[`company > 1211 > bs 1`] = `"cultivate bleeding-edge functionalities"`;
32+
exports[`company > 1211 > bs 1`] = `"cultivate bleeding-edge experiences"`;
3333

3434
exports[`company > 1211 > bsAdjective 1`] = `"plug-and-play"`;
3535

3636
exports[`company > 1211 > bsBuzz 1`] = `"cultivate"`;
3737

38-
exports[`company > 1211 > bsNoun 1`] = `"experiences"`;
38+
exports[`company > 1211 > bsNoun 1`] = `"methodologies"`;
3939

4040
exports[`company > 1211 > catchPhrase 1`] = `"Up-sized high-level success"`;
4141

test/__snapshots__/helpers.spec.ts.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ exports[`helpers > 42 > uniqueArray > with array 1`] = `
177177
]
178178
`;
179179

180+
exports[`helpers > 42 > weightedArrayElement > with array 1`] = `"sunny"`;
181+
182+
exports[`helpers > 42 > weightedArrayElement > with array with percentages 1`] = `"sunny"`;
183+
180184
exports[`helpers > 1211 > arrayElement > noArgs 1`] = `"c"`;
181185

182186
exports[`helpers > 1211 > arrayElement > with array 1`] = `"!"`;
@@ -368,6 +372,10 @@ exports[`helpers > 1211 > uniqueArray > with array 1`] = `
368372
]
369373
`;
370374

375+
exports[`helpers > 1211 > weightedArrayElement > with array 1`] = `"snowy"`;
376+
377+
exports[`helpers > 1211 > weightedArrayElement > with array with percentages 1`] = `"snowy"`;
378+
371379
exports[`helpers > 1337 > arrayElement > noArgs 1`] = `"a"`;
372380

373381
exports[`helpers > 1337 > arrayElement > with array 1`] = `"l"`;
@@ -541,3 +549,7 @@ exports[`helpers > 1337 > uniqueArray > with array 1`] = `
541549
"d",
542550
]
543551
`;
552+
553+
exports[`helpers > 1337 > weightedArrayElement > with array 1`] = `"sunny"`;
554+
555+
exports[`helpers > 1337 > weightedArrayElement > with array with percentages 1`] = `"sunny"`;

0 commit comments

Comments
 (0)