Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/chinclubi/faker.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak committed Oct 28, 2018
2 parents 3f7ff55 + 900f3d8 commit acfbf3b
Show file tree
Hide file tree
Showing 5 changed files with 502 additions and 91 deletions.
5 changes: 5 additions & 0 deletions conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"destination": "../doc/"
},

"source": {
"include": ["lib", "lib/image_providers"],
"exclude": ["lib/locales"]
},

"plugins": [
"plugins/markdown"
]
Expand Down
14 changes: 13 additions & 1 deletion lib/image.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/**
*
* @namespace faker.image
* @property {object} lorempixel - faker.image.lorempixel
* @property {object} unsplash - faker.image.unsplash
* @default Default provider is unsplash image provider
*/
var Image = function (faker) {

var self = this;
var Lorempixel = require('./image_providers/lorempixel');
var Unsplash = require('./image_providers/unsplash');

/**
* image
Expand Down Expand Up @@ -210,6 +215,13 @@ var Image = function (faker) {
var rawPrefix = 'data:image/svg+xml;charset=UTF-8,';
return rawPrefix + encodeURIComponent(svgString);
};

self.lorempixel = new Lorempixel(faker);
self.unsplash = new Unsplash(faker);

// Object.assign(self, self.unsplash);
// How to set default as unsplash? should be image.default?
}

module["exports"] = Image;

module["exports"] = Image;
199 changes: 199 additions & 0 deletions lib/image_providers/lorempixel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/**
*
* @namespace lorempixel
* @memberof faker.image
*/
var Lorempixel = function (faker) {

var self = this;

/**
* image
*
* @param {number} width
* @param {number} height
* @param {boolean} randomize
* @method faker.image.lorempixel.image
*/
self.image = function (width, height, randomize) {
var categories = ["abstract", "animals", "business", "cats", "city", "food", "nightlife", "fashion", "people", "nature", "sports", "technics", "transport"];
return self[faker.random.arrayElement(categories)](width, height, randomize);
};
/**
* avatar
*
* @method faker.image.lorempixel.avatar
*/
self.avatar = function () {
return faker.internet.avatar();
};
/**
* imageUrl
*
* @param {number} width
* @param {number} height
* @param {string} category
* @param {boolean} randomize
* @method faker.image.lorempixel.imageUrl
*/
self.imageUrl = function (width, height, category, randomize) {
var width = width || 640;
var height = height || 480;

var url ='http://lorempixel.com/' + width + '/' + height;
if (typeof category !== 'undefined') {
url += '/' + category;
}

if (randomize) {
url += '?' + faker.random.number()
}

return url;
};
/**
* abstract
*
* @param {number} width
* @param {number} height
* @param {boolean} randomize
* @method faker.image.lorempixel.abstract
*/
self.abstract = function (width, height, randomize) {
return faker.image.lorempixel.imageUrl(width, height, 'abstract', randomize);
};
/**
* animals
*
* @param {number} width
* @param {number} height
* @param {boolean} randomize
* @method faker.image.lorempixel.animals
*/
self.animals = function (width, height, randomize) {
return faker.image.lorempixel.imageUrl(width, height, 'animals', randomize);
};
/**
* business
*
* @param {number} width
* @param {number} height
* @param {boolean} randomize
* @method faker.image.lorempixel.business
*/
self.business = function (width, height, randomize) {
return faker.image.lorempixel.imageUrl(width, height, 'business', randomize);
};
/**
* cats
*
* @param {number} width
* @param {number} height
* @param {boolean} randomize
* @method faker.image.lorempixel.cats
*/
self.cats = function (width, height, randomize) {
return faker.image.lorempixel.imageUrl(width, height, 'cats', randomize);
};
/**
* city
*
* @param {number} width
* @param {number} height
* @param {boolean} randomize
* @method faker.image.lorempixel.city
*/
self.city = function (width, height, randomize) {
return faker.image.lorempixel.imageUrl(width, height, 'city', randomize);
};
/**
* food
*
* @param {number} width
* @param {number} height
* @param {boolean} randomize
* @method faker.image.lorempixel.food
*/
self.food = function (width, height, randomize) {
return faker.image.lorempixel.imageUrl(width, height, 'food', randomize);
};
/**
* nightlife
*
* @param {number} width
* @param {number} height
* @param {boolean} randomize
* @method faker.image.lorempixel.nightlife
*/
self.nightlife = function (width, height, randomize) {
return faker.image.lorempixel.imageUrl(width, height, 'nightlife', randomize);
};
/**
* fashion
*
* @param {number} width
* @param {number} height
* @param {boolean} randomize
* @method faker.image.lorempixel.fashion
*/
self.fashion = function (width, height, randomize) {
return faker.image.lorempixel.imageUrl(width, height, 'fashion', randomize);
};
/**
* people
*
* @param {number} width
* @param {number} height
* @param {boolean} randomize
* @method faker.image.lorempixel.people
*/
self.people = function (width, height, randomize) {
return faker.image.lorempixel.imageUrl(width, height, 'people', randomize);
};
/**
* nature
*
* @param {number} width
* @param {number} height
* @param {boolean} randomize
* @method faker.image.lorempixel.nature
*/
self.nature = function (width, height, randomize) {
return faker.image.lorempixel.imageUrl(width, height, 'nature', randomize);
};
/**
* sports
*
* @param {number} width
* @param {number} height
* @param {boolean} randomize
* @method faker.image.lorempixel.sports
*/
self.sports = function (width, height, randomize) {
return faker.image.lorempixel.imageUrl(width, height, 'sports', randomize);
};
/**
* technics
*
* @param {number} width
* @param {number} height
* @param {boolean} randomize
* @method faker.image.lorempixel.technics
*/
self.technics = function (width, height, randomize) {
return faker.image.lorempixel.imageUrl(width, height, 'technics', randomize);
};
/**
* transport
*
* @param {number} width
* @param {number} height
* @param {boolean} randomize
* @method faker.image.lorempixel.transport
*/
self.transport = function (width, height, randomize) {
return faker.image.lorempixel.imageUrl(width, height, 'transport', randomize);
}
}

module["exports"] = Lorempixel;
129 changes: 129 additions & 0 deletions lib/image_providers/unsplash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
*
* @namespace unsplash
* @memberof faker.image
*/
var Unsplash = function (faker) {

var self = this;
var categories = ["food", "nature", "people", "technology", "objects", "buildings"];

/**
* image
*
* @param {number} width
* @param {number} height
* @param {string} keyword
* @method faker.image.unsplash.image
* @description search image from unsplash
*/
self.image = function (width, height, keyword) {
return self.imageUrl(width, height, undefined, keyword);
};
/**
* avatar
*
* @method faker.image.unsplash.avatar
*/
self.avatar = function () {
return faker.internet.avatar();
};
/**
* imageUrl
*
* @param {number} width
* @param {number} height
* @param {string} category
* @param {string} keyword
* @method faker.image.unsplash.imageUrl
*/
self.imageUrl = function (width, height, category, keyword) {
var width = width || 640;
var height = height || 480;

var url ='https://source.unsplash.com';

if (typeof category !== 'undefined') {
url += '/category/' + category;
}

url += '/' + width + 'x' + height;

if (typeof keyword !== 'undefined') {
var keywordFormat = new RegExp('^([A-Za-z0-9].+,[A-Za-z0-9]+)$|^([A-Za-z0-9]+)$');
if (keywordFormat.test(keyword)) {
url += '?' + keyword;
}
}

return url;
};
/**
* food
*
* @param {number} width
* @param {number} height
* @param {string} keyword
* @method faker.image.unsplash.food
*/
self.food = function (width, height, keyword) {
return faker.image.unsplash.imageUrl(width, height, 'food', keyword);
};
/**
* people
*
* @param {number} width
* @param {number} height
* @param {string} keyword
* @method faker.image.unsplash.people
*/
self.people = function (width, height, keyword) {
return faker.image.unsplash.imageUrl(width, height, 'people', keyword);
};
/**
* nature
*
* @param {number} width
* @param {number} height
* @param {string} keyword
* @method faker.image.unsplash.nature
*/
self.nature = function (width, height, keyword) {
return faker.image.unsplash.imageUrl(width, height, 'nature', keyword);
};
/**
* technology
*
* @param {number} width
* @param {number} height
* @param {string} keyword
* @method faker.image.unsplash.technology
*/
self.technology = function (width, height, keyword) {
return faker.image.unsplash.imageUrl(width, height, 'technology', keyword);
};
/**
* objects
*
* @param {number} width
* @param {number} height
* @param {string} keyword
* @method faker.image.unsplash.objects
*/
self.objects = function (width, height, keyword) {
return faker.image.unsplash.imageUrl(width, height, 'objects', keyword);
};
/**
* buildings
*
* @param {number} width
* @param {number} height
* @param {string} keyword
* @method faker.image.unsplash.buildings
*/
self.buildings = function (width, height, keyword) {
return faker.image.unsplash.imageUrl(width, height, 'buildings', keyword);
};
}

module["exports"] = Unsplash;
Loading

0 comments on commit acfbf3b

Please sign in to comment.