Skip to content

Commit

Permalink
fix the avatar url in faker js
Browse files Browse the repository at this point in the history
  • Loading branch information
vedkribhu committed Jul 26, 2024
1 parent e1dfe7a commit b915db0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
34 changes: 33 additions & 1 deletion lib/superstring/dynamic-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ var faker = require('@faker-js/faker/locale/en'),
'/var/yp'
],

/**
* Copied over from: https://github.com/faker-js/faker/blob/next/src/modules/image/index.ts#L27
* Generates a random avatar from GitHub.
*
* @example 'https://avatars.githubusercontent.com/u/97165289'
*/
avatarGitHub = () => {
return `https://avatars.githubusercontent.com/u/${faker.datatype.number(100000000)}`;
},

/**
* Copied over from: https://github.com/faker-js/faker/blob/next/src/modules/image/index.ts#L27
* Generates a random avatar from
* `https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar`.
*
* @example 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/170.jpg'
*/
avatarLegacy = () => {
// eslint-disable-next-line max-len
return `https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/${faker.datatype.number(1249)}.jpg`;
},

// generators for $random* variables
dynamicGenerators = {
$guid: {
Expand Down Expand Up @@ -371,7 +393,17 @@ var faker = require('@faker-js/faker/locale/en'),

$randomAvatarImage: {
description: 'A random avatar image',
generator: faker.image.avatar
generator: () => {
// We have overridden the avatar generator to be similar to current
// latest version (v9) of faker.js. We don't want to upgrade to the latest
// version of faker.js as it is significantly big in size.
const avatarMethod = faker.random.arrayElement([
avatarLegacy,
avatarGitHub
]);

return avatarMethod();
}
},
$randomImageUrl: {
description: 'A URL for a random image',
Expand Down
7 changes: 7 additions & 0 deletions test/unit/dynamic-variables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,12 @@ describe('Dynamic variable', function () {
expect(directoryPath).to.not.be.undefined;
expect(directoryPath).to.not.be.null;
});

it('$randomAvatarImage returns a random avatar image', function () {
var avatarImage = dynamicVariables.$randomAvatarImage.generator();

expect(avatarImage).to.not.be.undefined;
expect(avatarImage).to.not.be.null;
});
});
});

0 comments on commit b915db0

Please sign in to comment.