Skip to content

Commit

Permalink
Merge pull request #251 from mradionov/master
Browse files Browse the repository at this point in the history
[api] Adding generators: faker.date.month(), faker.date.weekday()
  • Loading branch information
Marak committed Jul 21, 2015
2 parents 662fd80 + 7e1d18b commit eb23a6f
Show file tree
Hide file tree
Showing 11 changed files with 333 additions and 1 deletion.
34 changes: 33 additions & 1 deletion lib/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,39 @@ var _Date = function (faker) {
date.setTime(future);

return date;
}
};

self.month = function (options) {
options = options || {};

var type = 'wide';
if (options.abbr) {
type = 'abbr';
}
if (options.context && typeof faker.definitions.date.month[type + '_context'] !== 'undefined') {
type += '_context';
}

var source = faker.definitions.date.month[type];

return faker.random.arrayElement(source);
};

self.weekday = function (options) {
options = options || {};

var type = 'wide';
if (options.abbr) {
type = 'abbr';
}
if (options.context && typeof faker.definitions.date.weekday[type + '_context'] !== 'undefined') {
type += '_context';
}

var source = faker.definitions.date.weekday[type];

return faker.random.arrayElement(source);
};

return self;

Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function Faker (opts) {
"finance": ["account_type", "transaction_type", "currency"],
"internet": ["avatar_uri", "domain_suffix", "free_email", "password"],
"commerce": ["color", "department", "product_name", "price", "categories"],
"date": ["month", "weekday"],
"title": "",
"separator": ""
};
Expand Down
4 changes: 4 additions & 0 deletions lib/locales/en/date/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var date = {};
module["exports"] = date;
date.month = require("./month");
date.weekday = require("./weekday");
63 changes: 63 additions & 0 deletions lib/locales/en/date/month.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Source: http://unicode.org/cldr/trac/browser/tags/release-27/common/main/en.xml#L1799
module["exports"] = {
wide: [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
// Property "wide_context" is optional, if not set then "wide" will be used instead
// It is used to specify a word in context, which may differ from a stand-alone word
wide_context: [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
abbr: [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
],
// Property "abbr_context" is optional, if not set then "abbr" will be used instead
// It is used to specify a word in context, which may differ from a stand-alone word
abbr_context: [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
]
};
43 changes: 43 additions & 0 deletions lib/locales/en/date/weekday.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Source: http://unicode.org/cldr/trac/browser/tags/release-27/common/main/en.xml#L1847
module["exports"] = {
wide: [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
],
// Property "wide_context" is optional, if not set then "wide" will be used instead
// It is used to specify a word in context, which may differ from a stand-alone word
wide_context: [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
],
abbr: [
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
],
// Property "abbr_context" is optional, if not set then "abbr" will be used instead
// It is used to specify a word in context, which may differ from a stand-alone word
abbr_context: [
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
]
};
1 change: 1 addition & 0 deletions lib/locales/en/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ en.team = require("./team");
en.hacker = require("./hacker");
en.app = require("./app");
en.finance = require("./finance");
en.date = require("./date");
4 changes: 4 additions & 0 deletions lib/locales/ru/date/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var date = {};
module["exports"] = date;
date.month = require("./month");
date.weekday = require("./weekday");
59 changes: 59 additions & 0 deletions lib/locales/ru/date/month.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// source: http://unicode.org/cldr/trac/browser/tags/release-27/common/main/ru.xml#L1734
module["exports"] = {
wide: [
"январь",
"февраль",
"март",
"апрель",
"май",
"июнь",
"июль",
"август",
"сентябрь",
"октябрь",
"ноябрь",
"декабрь"
],
wide_context: [
"января",
"февраля",
"марта",
"апреля",
"мая",
"июня",
"июля",
"августа",
"сентября",
"октября",
"ноября",
"декабря"
],
abbr: [
"янв.",
"февр.",
"март",
"апр.",
"май",
"июнь",
"июль",
"авг.",
"сент.",
"окт.",
"нояб.",
"дек."
],
abbr_context: [
"янв.",
"февр.",
"марта",
"апр.",
"мая",
"июня",
"июля",
"авг.",
"сент.",
"окт.",
"нояб.",
"дек."
]
};
39 changes: 39 additions & 0 deletions lib/locales/ru/date/weekday.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// source: http://unicode.org/cldr/trac/browser/tags/release-27/common/main/ru.xml#L1825
module["exports"] = {
wide: [
"Воскресенье",
"Понедельник",
"Вторник",
"Среда",
"Четверг",
"Пятница",
"Суббота"
],
wide_context: [
"воскресенье",
"понедельник",
"вторник",
"среда",
"четверг",
"пятница",
"суббота"
],
abbr: [
"Вс",
"Пн",
"Вт",
"Ср",
"Чт",
"Пт",
"Сб"
],
abbr_context: [
"вс",
"пн",
"вт",
"ср",
"чт",
"пт",
"сб"
]
};
1 change: 1 addition & 0 deletions lib/locales/ru/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ ru.name = require("./name");
ru.phone_number = require("./phone_number");
ru.commerce = require("./commerce");
ru.company = require("./company");
ru.date = require("./date");
85 changes: 85 additions & 0 deletions test/date.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,89 @@ describe("date.js", function () {
assert.ok(date > from && date < to);
});
});

describe("month()", function () {
it("returns random value from date.month.wide array by default", function () {
var month = faker.date.month();
assert.ok(faker.definitions.date.month.wide.indexOf(month) !== -1);
});

it("returns random value from date.month.wide_context array for context option", function () {
var month = faker.date.month({ context: true });
assert.ok(faker.definitions.date.month.wide_context.indexOf(month) !== -1);
});

it("returns random value from date.month.abbr array for abbr option", function () {
var month = faker.date.month({ abbr: true });
assert.ok(faker.definitions.date.month.abbr.indexOf(month) !== -1);
});

it("returns random value from date.month.abbr_context array for abbr and context option", function () {
var month = faker.date.month({ abbr: true, context: true });
assert.ok(faker.definitions.date.month.abbr_context.indexOf(month) !== -1);
});

it("returns random value from date.month.wide array for context option when date.month.wide_context array is missing", function () {
var backup_wide_context = faker.definitions.date.month.wide_context;
faker.definitions.date.month.wide_context = undefined;

var month = faker.date.month({ context: true });
assert.ok(faker.definitions.date.month.wide.indexOf(month) !== -1);

faker.definitions.date.month.wide_context = backup_wide_context;
});

it("returns random value from date.month.abbr array for abbr and context option when date.month.abbr_context array is missing", function () {
var backup_abbr_context = faker.definitions.date.month.abbr_context;
faker.definitions.date.month.abbr_context = undefined;

var month = faker.date.month({ abbr: true, context: true });
assert.ok(faker.definitions.date.month.abbr.indexOf(month) !== -1);

faker.definitions.date.month.abbr_context = backup_abbr_context;
});
});

describe("weekday()", function () {
it("returns random value from date.weekday.wide array by default", function () {
var weekday = faker.date.weekday();
assert.ok(faker.definitions.date.weekday.wide.indexOf(weekday) !== -1);
});

it("returns random value from date.weekday.wide_context array for context option", function () {
var weekday = faker.date.weekday({ context: true });
assert.ok(faker.definitions.date.weekday.wide_context.indexOf(weekday) !== -1);
});

it("returns random value from date.weekday.abbr array for abbr option", function () {
var weekday = faker.date.weekday({ abbr: true });
assert.ok(faker.definitions.date.weekday.abbr.indexOf(weekday) !== -1);
});

it("returns random value from date.weekday.abbr_context array for abbr and context option", function () {
var weekday = faker.date.weekday({ abbr: true, context: true });
assert.ok(faker.definitions.date.weekday.abbr_context.indexOf(weekday) !== -1);
});

it("returns random value from date.weekday.wide array for context option when date.weekday.wide_context array is missing", function () {
var backup_wide_context = faker.definitions.date.weekday.wide_context;
faker.definitions.date.weekday.wide_context = undefined;

var weekday = faker.date.weekday({ context: true });
assert.ok(faker.definitions.date.weekday.wide.indexOf(weekday) !== -1);

faker.definitions.date.weekday.wide_context = backup_wide_context;
});

it("returns random value from date.weekday.abbr array for abbr and context option when date.weekday.abbr_context array is missing", function () {
var backup_abbr_context = faker.definitions.date.weekday.abbr_context;
faker.definitions.date.weekday.abbr_context = undefined;

var weekday = faker.date.weekday({ abbr: true, context: true });
assert.ok(faker.definitions.date.weekday.abbr.indexOf(weekday) !== -1);

faker.definitions.date.weekday.abbr_context = backup_abbr_context;
});
});

});

0 comments on commit eb23a6f

Please sign in to comment.