Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Smee committed May 6, 2022
2 parents 32b84de + 2c666fb commit 6f7dcc6
Show file tree
Hide file tree
Showing 9 changed files with 575 additions and 26 deletions.
48 changes: 48 additions & 0 deletions packages/falso/src/lib/bic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { fake } from './core/core';
import { randSwift, SwiftOptions } from './swift';

/**
* Generate a random swift bic code.
*
* @category finance
*
* @example
*
* randBic()
*
* @example
*
* randBic({ bankCode: 'DEUT' }) // bank code of Deutsche Bank
*
* @example
*
* randBic({ countryCode: 'DE' }) // country code with ISO country code
*
* @example
*
* randBic({ locationCode: 'MM' }) // location code for Milan MM for Frankfurt FF and etc
*
* @example
*
* randBic({ branchCode: '250' }) // bank branch code
*
* @example
*
* randBic({ fillBranchCode: true }) // bank branch code filled with XXX
*
* @example
*
* randBic({ length: 10 })
*
*/
export function randBic<Options extends SwiftOptions = never>(
options?: Options
) {
const _options = { ...options, length: undefined };

const factory: () => string = () => {
return randSwift(_options).toString();
};

return fake(factory, options);
}
7 changes: 2 additions & 5 deletions packages/falso/src/lib/hexa-decimal.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { fake, FakeOptions } from './core/core';
import { randBoolean } from './boolean';

const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const letters = ['A', 'B', 'C', 'D', 'E', 'F'];
import { randNumber } from './number';

function generator() {
return '' + (randBoolean() ? fake(digits) : fake(letters));
return randNumber({min:0, max:15}).toString(16);
}

/**
Expand Down
39 changes: 30 additions & 9 deletions packages/falso/src/lib/iban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { randCountryCode } from './country-code';
import { randNumber } from './number';
import { randAlphaNumeric } from './alpha-numeric';

export interface IbanOptions extends FakeOptions {
countryCode?: string; // [A-Z]{2}
}

/**
* Generate a random ibn.
* Generate a random IBAN number.
*
* @category finance
*
Expand All @@ -14,23 +18,40 @@ import { randAlphaNumeric } from './alpha-numeric';
*
* @example
*
* randSwift({ countryCode: 'DE' }) // country code with ISO country code
*
* @example
*
* randIban({ length: 10 })
*
* @example
*
* randIban({ length: 10, priority: 'unique' }) // default is 'length' ('length' | 'unique')
*
*/
export function randIban<Options extends FakeOptions = never>(
export function randIban<Options extends IbanOptions = never>(
options?: Options
) {
return fake(() => {
let iban = randCountryCode();
if (options?.countryCode && options?.countryCode?.length !== 2) {
throw new Error(
'country code should be valid ISO 3166-1 alpha-2 two-letter country code, for example: DE'
);
}

const factory: () => string = () => {
const countryCode = options?.countryCode ?? randCountryCode();

const checkDigits = randNumber({ min: 10, max: 99 }).toString();

const bban = Array(randNumber({ min: 12, max: 30 }))
.fill('#')
.join('')
.replace(/#/g, () => {
return randAlphaNumeric().toString();
});

for (let i = 0; i < randNumber({ min: 14, max: 28 }); i++) {
iban += randAlphaNumeric();
}
return `${countryCode}${checkDigits}${bban}`.toUpperCase();
};

return iban;
}, options);
return fake(factory, options);
}
100 changes: 100 additions & 0 deletions packages/falso/src/lib/swift.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { FakeOptions, fake } from './core/core';
import { randCountryCode } from './country-code';
import { randAlphaNumeric } from './alpha-numeric';
import { randAlpha } from './alpha';

export interface SwiftOptions extends FakeOptions {
bankCode?: string; // [A-Z]{4}
countryCode?: string; // [A-Z]{2}
locationCode?: string; // [A-Z0-9]{2}
branchCode?: string; // [A-Z0-9]{3} optional
fillBranchCode?: boolean; // fill branchCode using XXX symbols
}

/**
* Generate a random swift bic code.
*
* @category finance
*
* @example
*
* randSwift()
*
* @example
*
* randSwift({ bankCode: 'DEUT' }) // bank code of Deutsche Bank
*
* @example
*
* randSwift({ countryCode: 'DE' }) // country code with ISO country code
*
* @example
*
* randSwift({ locationCode: 'MM' }) // location code for Milan MM or for Frankfurt FF and etc
*
* @example
*
* randSwift({ branchCode: '250' }) // bank branch code
*
* @example
*
* randSwift({ fillBranchCode: true }) // bank branch code filled with XXX
*
* @example
*
* randSwift({ length: 10 })
*
*/
export function randSwift<Options extends SwiftOptions = never>(
options?: Options
) {
if (options?.bankCode && options?.bankCode?.length !== 4) {
throw new Error('bank code should be valid 4 letters. For example DEUT');
}

if (options?.countryCode && options?.countryCode?.length !== 2) {
throw new Error(
'country code should be valid ISO 3166-1 alpha-2 two-letter country code, for example: DE'
);
}

if (options?.locationCode && options?.locationCode?.length !== 2) {
throw new Error(
'location code should be valid 2 characters, like FF or MM'
);
}

if (options?.branchCode && options?.branchCode?.length !== 3) {
throw new Error(
'branch code should be valid 3 alpha numberic characters, like XXX or 250'
);
}

const factory: () => string = () => {
const bankCode: string =
options?.bankCode ??
'####'.replace(/#/g, () => {
return randAlpha();
});

const countryCode: string = options?.countryCode ?? randCountryCode();

const locationCode: string =
options?.locationCode ??
'##'.replace(/#/g, () => {
return randAlpha();
});

const branchCode: string =
options?.branchCode ??
'###'.replace(/#/g, () => {
return randAlphaNumeric().toString();
});

return `${bankCode}${countryCode}${locationCode}${
options?.fillBranchCode ? 'XXX' : branchCode
}`.toUpperCase();
};

return fake(factory, options);
}
160 changes: 148 additions & 12 deletions packages/falso/src/lib/vehicle-model.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,158 @@
{
"data": [
"Alpine",
"Land Cruiser",
"Grand Cherokee",
"296 GTB",
"4Runner",
"500",
"718",
"911",
"A-Class",
"A2",
"A3",
"A4",
"Golf",
"A6",
"A7",
"A8",
"Accord",
"Expedition",
"Camry",
"Jetta",
"Taurus",
"Alpine",
"Arteon",
"Atlas",
"Avalon",
"Aventador",
"Aviator",
"Blazer",
"Bronco",
"C-Class",
"C-HR",
"Camaro",
"Prius",
"CX-9",
"Wrangler",
"Camry",
"Cayenne",
"Chiron",
"Civic",
"Clarity",
"Clubman",
"Colorado",
"Corolla",
"Corsair",
"Corvette",
"Countryman",
"CR-V",
"CT-4",
"CT-5",
"CX-9",
"Duster",
"e-tron",
"E-Class",
"Edge",
"Elantra",
"Escalade",
"EQS",
"EV-6",
"Expedition",
"Explorer",
"F-150",
"Fiesta",
"Forester",
"Ghibli",
"Giulia",
"GR86",
"Grand Cherokee",
"Grecale",
"Golf",
"Highlander",
"HR-V",
"Huracan",
"ID.4",
"Impreza",
"Insight",
"Ioniq",
"Jetta",
"John Cooper Works",
"Kona",
"Land Cruiser",
"Legacy",
"Levante",
"Logan",
"Lyriq",
"M3",
"M4",
"M5",
"Macan",
"Malibu",
"Maverick",
"Mirai",
"Model 3",
"Model S",
"Model X",
"Model Y",
"Mustang",
"Nautilus",
"Navigator",
"Niro",
"Outback",
"Odyssey",
"Palisade",
"Panamera",
"Passat",
"Passport",
"Portofino",
"Prius",
"Q3",
"Q4",
"Q5",
"Q6",
"Q7",
"Q8",
"Quattroporte",
"Ranger",
"R8",
"RAV4",
"Rio",
"Roma",
"S-Class",
"Sandero",
"Santa fe",
"Sequoia",
"Sentra",
"Silverado"
"Sienna",
"Silverado",
"Sonata",
"Sorento",
"Spark",
"Sportage",
"Spring",
"Stelvio",
"Stinger",
"Suburban",
"Super Duty",
"Supra",
"Tacoma",
"Tahoe",
"Tacoma",
"Taos",
"Taurus",
"Taycan",
"Tiguan",
"Tonale",
"Touareg",
"Trailblazer",
"TT",
"Tucson",
"Tundra",
"Urus",
"Veloster",
"Venza",
"Veyron",
"Wrangler",
"X1",
"X2",
"X3",
"X4",
"X5",
"X6",
"X7",
"XT4",
"XT5",
"Yaris",
"Z4"
]
}
Loading

0 comments on commit 6f7dcc6

Please sign in to comment.