diff options
| author | Marak <[email protected]> | 2015-07-21 01:01:12 -0700 |
|---|---|---|
| committer | Marak <[email protected]> | 2015-07-21 01:01:12 -0700 |
| commit | eb23a6ffc3a1a753e20049fb0cee2722b8627264 (patch) | |
| tree | f03345604887f7f792b5f73735409821147c9d56 | |
| parent | 662fd808eb78f241aee859ba6011e883a6ca1261 (diff) | |
| parent | 7e1d18b7b897921e50f810bd7397dcec6f5bd40c (diff) | |
| download | faker-eb23a6ffc3a1a753e20049fb0cee2722b8627264.tar.xz faker-eb23a6ffc3a1a753e20049fb0cee2722b8627264.zip | |
Merge pull request #251 from mradionov/master
[api] Adding generators: faker.date.month(), faker.date.weekday()
| -rw-r--r-- | lib/date.js | 34 | ||||
| -rw-r--r-- | lib/index.js | 1 | ||||
| -rw-r--r-- | lib/locales/en/date/index.js | 4 | ||||
| -rw-r--r-- | lib/locales/en/date/month.js | 63 | ||||
| -rw-r--r-- | lib/locales/en/date/weekday.js | 43 | ||||
| -rw-r--r-- | lib/locales/en/index.js | 1 | ||||
| -rw-r--r-- | lib/locales/ru/date/index.js | 4 | ||||
| -rw-r--r-- | lib/locales/ru/date/month.js | 59 | ||||
| -rw-r--r-- | lib/locales/ru/date/weekday.js | 39 | ||||
| -rw-r--r-- | lib/locales/ru/index.js | 1 | ||||
| -rw-r--r-- | test/date.unit.js | 85 |
11 files changed, 333 insertions, 1 deletions
diff --git a/lib/date.js b/lib/date.js index e868580d..2c53a4e4 100644 --- a/lib/date.js +++ b/lib/date.js @@ -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; diff --git a/lib/index.js b/lib/index.js index 07a0f825..2ebd6028 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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": "" }; diff --git a/lib/locales/en/date/index.js b/lib/locales/en/date/index.js new file mode 100644 index 00000000..8c45d3f6 --- /dev/null +++ b/lib/locales/en/date/index.js @@ -0,0 +1,4 @@ +var date = {}; +module["exports"] = date; +date.month = require("./month"); +date.weekday = require("./weekday"); diff --git a/lib/locales/en/date/month.js b/lib/locales/en/date/month.js new file mode 100644 index 00000000..14416723 --- /dev/null +++ b/lib/locales/en/date/month.js @@ -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" + ] +}; diff --git a/lib/locales/en/date/weekday.js b/lib/locales/en/date/weekday.js new file mode 100644 index 00000000..f16cb0bf --- /dev/null +++ b/lib/locales/en/date/weekday.js @@ -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" + ] +}; diff --git a/lib/locales/en/index.js b/lib/locales/en/index.js index e66d19a7..95708a0b 100644 --- a/lib/locales/en/index.js +++ b/lib/locales/en/index.js @@ -16,3 +16,4 @@ en.team = require("./team"); en.hacker = require("./hacker"); en.app = require("./app"); en.finance = require("./finance"); +en.date = require("./date"); diff --git a/lib/locales/ru/date/index.js b/lib/locales/ru/date/index.js new file mode 100644 index 00000000..8c45d3f6 --- /dev/null +++ b/lib/locales/ru/date/index.js @@ -0,0 +1,4 @@ +var date = {}; +module["exports"] = date; +date.month = require("./month"); +date.weekday = require("./weekday"); diff --git a/lib/locales/ru/date/month.js b/lib/locales/ru/date/month.js new file mode 100644 index 00000000..5deda00c --- /dev/null +++ b/lib/locales/ru/date/month.js @@ -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: [ + "янв.", + "февр.", + "марта", + "апр.", + "мая", + "июня", + "июля", + "авг.", + "сент.", + "окт.", + "нояб.", + "дек." + ] +}; diff --git a/lib/locales/ru/date/weekday.js b/lib/locales/ru/date/weekday.js new file mode 100644 index 00000000..fc91073e --- /dev/null +++ b/lib/locales/ru/date/weekday.js @@ -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: [ + "вс", + "пн", + "вт", + "ср", + "чт", + "пт", + "сб" + ] +}; diff --git a/lib/locales/ru/index.js b/lib/locales/ru/index.js index c822cf0e..9c2d7660 100644 --- a/lib/locales/ru/index.js +++ b/lib/locales/ru/index.js @@ -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"); diff --git a/test/date.unit.js b/test/date.unit.js index cc0cddc7..ffbc3201 100644 --- a/test/date.unit.js +++ b/test/date.unit.js @@ -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; + }); + }); + }); |
