gengojs-default-api
Advanced tools
Comparing version 0.0.2 to 0.0.3
324
lib/index.js
import cldr from 'cldr'; | ||
import _ from 'lodash'; | ||
import d from 'debug'; | ||
var debug = d('default-api'); | ||
import debug from 'gengojs-debug'; | ||
var log = debug('api'); | ||
/* Class API */ | ||
class API { | ||
constructor(_this) { | ||
this.options = _this.options; | ||
this.context = _this; | ||
} | ||
/* Sets the API*/ | ||
constructor(core) { | ||
log.debug(`class: ${API.name}`, `process: constructor`); | ||
this.options = core.options; | ||
this.context = core; | ||
} | ||
/* Sets the API*/ | ||
set() { | ||
var _this = this.context; | ||
var i18n = function() {}; | ||
var l10n = function() {}; | ||
var options = this.options.api; | ||
_.assign((options.header = {}), this.options.header); | ||
debug('options exists:', !(!options)); | ||
/** | ||
log.debug(`class: ${API.name}`, `process: set`); | ||
var core = this.context; | ||
var i18n = function() {}; | ||
var l10n = function() {}; | ||
var options = this.options.api; | ||
_.assign((options.header = {}), this.options.header); | ||
debug('api', 'info', 'options exists:', !(!options)); | ||
/** | ||
* @method i18n | ||
@@ -63,4 +66,4 @@ * @description I18ns the arguments. | ||
* @example <caption>All notations with Message Format.</caption> | ||
* // See '{@link https://github.com/thetalecrafter/ | ||
message-format|message-format}' for documentation. | ||
* // See [message-format](https://github.com/thetalecrafter/ | ||
message-format) for documentation. | ||
* // See updated docs at README: | ||
@@ -87,40 +90,40 @@ * | ||
i18n[options.global] = function(...args) { | ||
debug('process:', 'i18n:', 'globalize'); | ||
return _this.parse.apply(_this, args); | ||
}; | ||
/** | ||
* @method language | ||
* @description Returns the name of the current locale. | ||
* @param {string} id The locale to change. | ||
* | ||
* @example <caption>Get the current language.</caption> | ||
* | ||
* // assuming locale === 'en-us' | ||
* // returns 'American English' | ||
* __.languages(); | ||
* | ||
* @example <caption>Get the current language in another locale. </caption> | ||
* | ||
* // assuming locale === 'en-us' | ||
* // returns 'English' | ||
* __.language('en'); | ||
* | ||
* // returns 'Japanese' | ||
* __.language('ja'); | ||
* | ||
* @return {String} Then i18ned string. | ||
* @public | ||
*/ | ||
i18n.language = function(id) { | ||
debug('process:', 'i18n:', 'language'); | ||
// de-normalize locale | ||
var locale = _this.header.getLocale(); | ||
locale = locale.toLowerCase().replace('-', '_'); | ||
// denormalize id | ||
id = id ? id.toLowerCase().replace('_', '-') : locale; | ||
// store the languages | ||
return cldr.extractLanguageDisplayNames(locale)[id]; | ||
}; | ||
/** | ||
i18n[options.global] = function(...args) { | ||
log.debug(`class: ${API.name}`, `process: i18n`); | ||
return core.parse.apply(core, args); | ||
}; | ||
/** | ||
* @method language | ||
* @description Returns the name of the current locale. | ||
* @param {string} id The locale to change. | ||
* | ||
* @example <caption>Get the current language.</caption> | ||
* | ||
* // assuming locale === 'en-us' | ||
* // returns 'American English' | ||
* __.languages(); | ||
* | ||
* @example <caption>Get the current language in another locale. </caption> | ||
* | ||
* // assuming locale === 'en-us' | ||
* // returns 'English' | ||
* __.language('en'); | ||
* | ||
* // returns 'Japanese' | ||
* __.language('ja'); | ||
* | ||
* @return {String} Then i18ned string. | ||
* @public | ||
*/ | ||
i18n.language = function(id) { | ||
log.debug(`class: ${API.name}`, `process: i18n.languge`); | ||
// de-normalize locale | ||
var locale = core.header.getLocale(); | ||
locale = locale.toLowerCase().replace('-', '_'); | ||
// denormalize id | ||
id = id ? id.toLowerCase().replace('_', '-') : locale; | ||
// store the languages | ||
return cldr.extractLanguageDisplayNames(locale)[id]; | ||
}; | ||
/** | ||
* @method languages | ||
@@ -159,108 +162,102 @@ * @description Returns the names of the supported locale. | ||
*/ | ||
i18n.languages = (arg, supported) => { | ||
debug('process:', 'i18n:', 'languages'); | ||
var _supported = []; | ||
supported = (_.isArray(arg) ? arg : supported) || | ||
options.header.supported; | ||
arg = _.isArray(arg) ? undefined : arg; | ||
supported.forEach(locale => { | ||
arg = arg ? arg.toLowerCase() : | ||
_this.header.getLocale(); | ||
arg = arg.replace('_', '-'); | ||
// de-normalize locales | ||
locale = locale.toLowerCase().replace('-', '_'); | ||
// store the languages | ||
_supported.push(cldr.extractLanguageDisplayNames(arg)[locale]); | ||
}, _this); | ||
return _supported; | ||
}; | ||
i18n.languages = (arg, supported) => { | ||
log.debug(`class: ${API.name}`, `process: i18n.languges`); | ||
var _supported = []; | ||
supported = (_.isArray(arg) ? arg : supported) || | ||
options.header.supported; | ||
arg = _.isArray(arg) ? undefined : arg; | ||
supported.forEach(locale => { | ||
arg = arg ? arg.toLowerCase() : | ||
core.header.getLocale(); | ||
arg = arg.replace('_', '-'); | ||
// de-normalize locales | ||
locale = locale.toLowerCase().replace('-', '_'); | ||
// store the languages | ||
_supported.push(cldr.extractLanguageDisplayNames(arg)[locale]); | ||
}, core); | ||
return _supported; | ||
}; | ||
/** | ||
* @method locale | ||
* @description Sets or gets the locale. | ||
* @param {String} locale The locale to set or get. | ||
* | ||
* @example <caption>Get the current locale.</caption> | ||
* | ||
* // Assuming locale === 'en-us' | ||
* // returns 'en-us' | ||
* __.locale() | ||
* | ||
* @example <caption>Set the locale.</caption> | ||
* | ||
* // Asumming locale === 'en-us' | ||
* // sets and returns 'ja' | ||
* __.locale('ja') | ||
* | ||
* @return {String} The locale. | ||
* @public | ||
*/ | ||
i18n.locale = (locale) => { | ||
debug('process:', 'i18n:', 'locale'); | ||
return locale ? _this.header.setLocale(locale) : | ||
_this.header.detectLocale(); | ||
}; | ||
/** | ||
* @method locale | ||
* @description Sets or gets the locale. | ||
* @param {String} locale The locale to set or get. | ||
* | ||
* @example <caption>Get the current locale.</caption> | ||
* | ||
* // Assuming locale === 'en-us' | ||
* // returns 'en-us' | ||
* __.locale() | ||
* | ||
* @example <caption>Set the locale.</caption> | ||
* | ||
* // Asumming locale === 'en-us' | ||
* // sets and returns 'ja' | ||
* __.locale('ja') | ||
* | ||
* @return {String} The locale. | ||
* @public | ||
*/ | ||
i18n.locale = (locale) => { | ||
debug('api', 'debug', `class: ${API.name}`, `process: i18n.locale`); | ||
return locale ? | ||
core.header.setLocale(locale) : | ||
core.header.detectLocale ? | ||
core.header.detectLocale() : | ||
core.header.getLocale(); | ||
}; | ||
/** | ||
* @method locales | ||
* @description Returns the locales. | ||
* | ||
* @example <caption>Get the current locale.</caption> | ||
* | ||
* // Returns the locales from the catalog | ||
* __.locale() | ||
* | ||
* @return {String} The locale. | ||
* @public | ||
*/ | ||
i18n.locales = () => { | ||
debug('process:', 'i18n:', 'locales'); | ||
return Object.keys(_this.backend.catalog()); | ||
}; | ||
/** | ||
* @description Get the cldr. | ||
* @return {CLDR} The instance of cldr. | ||
* @public | ||
*/ | ||
i18n.cldr = () => { | ||
log.debug(`class: ${API.name}`, `process: i18n.cldr`); | ||
return cldr; | ||
}; | ||
/** | ||
* @description Get the cldr. | ||
* @return {CLDR} The instance of cldr. | ||
* @public | ||
*/ | ||
i18n.cldr = () => { | ||
debug('process:', 'i18n:', 'cldr'); | ||
return cldr; | ||
}; | ||
/** | ||
* Returns the catalog | ||
* @param {String} locale The locale to find | ||
* @return {Object} The catalog | ||
*/ | ||
i18n.catalog = (locale) => { | ||
log.debug(`class: ${API.name}`, `process: i18n.catalog`); | ||
return core.backend.catalog(locale); | ||
}; | ||
/** | ||
* @method l10n | ||
* @description Localizes date, time and numbers. | ||
* See [Tokei](https://github.com/iwatakeshi/tokei) for documentation. | ||
* Note: You can change ID for l10n. See Configuration. | ||
* @param {String} locale The locale to override. | ||
* @return {Tokei} The instance of Tokei. | ||
* @public | ||
*/ | ||
l10n[options.localize] = function(...args) { | ||
log.debug(`class: ${API.name}`, `process: i10n`); | ||
return core.localize.apply(core, args); | ||
}; | ||
return { | ||
i18n: i18n, | ||
l10n: l10n | ||
}; | ||
} | ||
/** | ||
* Returns the catalog | ||
* @param {String} locale The locale to find | ||
* @return {Object} The catalog | ||
* Returns the API | ||
* @returns The API | ||
*/ | ||
i18n.catalog = (locale) => { | ||
debug('process:', 'i18n:', 'catalog'); | ||
return _this.backend.catalog(locale); | ||
}; | ||
/** | ||
* @method l10n | ||
* @description Localizes date, time and numbers. | ||
* See {@link https://github.com/iwatakeshi/tokei|Tokei} for documentation. | ||
* Note: You can change ID for l10n. See Configuration. | ||
* @param {String} locale The locale to override. | ||
* @return {Tokei} The instance of Tokei. | ||
* @public | ||
get() { | ||
log.debug(`class: ${API.name}`, `process: get`); | ||
return this.apply({}); | ||
} | ||
/* | ||
* Applies the API to any object | ||
* @private | ||
*/ | ||
l10n[options.localize] = function(...args) { | ||
debug('process:', 'l10n:', 'localize'); | ||
return _this.localize.apply(_this, args); | ||
}; | ||
return { | ||
i18n: i18n, | ||
l10n: l10n | ||
}; | ||
} | ||
/* Get the API */ | ||
get() { | ||
return this.apply({}); | ||
} | ||
/* Private: Applies the API to any object */ | ||
apply(object) { | ||
var _this = this.context; | ||
log.debug(`class: ${API.name}`, `process: apply`); | ||
var core = this.context; | ||
_.forEach(this.set(), function(item, key) { | ||
@@ -272,5 +269,5 @@ switch (key) { | ||
if (subkey === this.options.api.global) | ||
object[subkey] = api.bind(_this); | ||
object[subkey] = api.bind(core); | ||
else | ||
object[this.options.api.global][subkey] = api.bind(_this); | ||
object[this.options.api.global][subkey] = api.bind(core); | ||
} | ||
@@ -283,3 +280,3 @@ }, this); | ||
if (subkey === this.options.api.localize) | ||
object[subkey] = api.bind(_this); | ||
object[subkey] = api.bind(core); | ||
} | ||
@@ -290,3 +287,3 @@ }, this); | ||
}, this); | ||
debug('API exists:', | ||
log.debug(`class: ${API.name}`, 'API exists:', | ||
_.has(object, this.options.api.global) && | ||
@@ -298,16 +295,15 @@ _.has(object, this.options.api.localize)); | ||
export | ||
default () => { | ||
export default function api() { | ||
'use strict'; | ||
return { | ||
main: function() { | ||
main: function ship() { | ||
var object = arguments[0] || arguments[1] || {}; | ||
debug('object exists:', !!object); | ||
this.api = new API(this).apply(object); | ||
log.debug('object exists:', !!object); | ||
return new API(this).apply(object); | ||
}, | ||
package: _.merge({ | ||
type: 'api' | ||
}, require('./package')), | ||
defaults: require('./defaults') | ||
}, require('../package')), | ||
defaults: require('../defaults') | ||
}; | ||
}; | ||
} |
{ | ||
"name": "gengojs-default-api", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "The default router plugin for gengojs", | ||
"main": "index.js", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "grunt && mocha ./test/*.js" | ||
"test": "gulp test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/iwatakeshi/gengojs-default-api.git" | ||
"url": "https://github.com/gengojs/plugin-api.git" | ||
}, | ||
@@ -18,13 +18,21 @@ "author": { | ||
"bugs": { | ||
"url": "https://github.com/iwatakeshi/gengojs-default-api/issues" | ||
"url": "https://github.com/gengojs/plugin-api/issues" | ||
}, | ||
"homepage": "https://github.com/iwatakeshi/gengojs-default-api", | ||
"homepage": "https://github.com/gengojs/plugin-api", | ||
"devDependencies": { | ||
"chai": "^3.2.0", | ||
"gengojs-core": "0.0.3", | ||
"grunt": "^0.4.5", | ||
"grunt-babel": "^5.0.0", | ||
"grunt-contrib-jshint": "^0.11.2", | ||
"load-grunt-tasks": "^3.1.0", | ||
"mocha": "^2.2.4" | ||
"gengojs-core": "^0.3.8", | ||
"gulp": "^3.9.0", | ||
"gulp-babel": "^5.2.1", | ||
"gulp-changelog": "^1.0.0", | ||
"gulp-gh-pages": "^0.5.2", | ||
"gulp-jsbeautify": "^0.1.1", | ||
"gulp-jshint": "^1.11.2", | ||
"gulp-mocha": "^2.1.3", | ||
"gulp-shell": "^0.4.3", | ||
"gulp-sourcemaps": "^1.6.0", | ||
"jshint-stylish": "^2.0.1", | ||
"mr-doc": "git+https://github.com/mr-doc/mr-doc.git", | ||
"mr-doc-theme-cayman": "^1.0.3", | ||
"rimraf": "^2.4.3" | ||
}, | ||
@@ -34,4 +42,5 @@ "dependencies": { | ||
"debug": "^2.1.3", | ||
"gengojs-debug": "^2.0.4", | ||
"lodash": "^3.8.0" | ||
} | ||
} |
257
README.md
@@ -5,6 +5,8 @@ # gengojs-default-api | ||
This module will be used for [gengo.js](https://github.com/iwatakeshi/gengojs). | ||
[![Build Status](https://travis-ci.org/gengojs/plugin-api.svg?branch=master)](https://travis-ci.org/gengojs/plugin-api) | ||
Note: The API examples defined are respect to the [default parser](https://github.com/iwatakeshi/gengojs-default-parser). | ||
This module will be used for [gengo.js](https://github.com/gengojs/gengojs). | ||
Note: The API examples defined are respect to the [default parser](https://github.com/gengojs/plugin-parser). | ||
An example usage with options is: | ||
@@ -33,3 +35,6 @@ | ||
## Documentation | ||
See [documentation](https://gengojs.github.io/plugin-api) | ||
## Options | ||
@@ -51,3 +56,3 @@ | ||
* `this.header.setLocale(locale:String)` from `Header` class | ||
* `this.header.detectLocale(locale:String)` from `Header` class | ||
* `this.header.detectLocale(locale:String)` from `Header` class (optional) | ||
* `supported:Array` from `Header` options | ||
@@ -69,248 +74,2 @@ * `this.backend.catalog(locale:String)` from `Backend` class | ||
## API | ||
* * * | ||
### i18n(arg) | ||
I18ns the arguments. | ||
Note: You can change ID for i18n. See Options. | ||
**Parameters** | ||
**arg**: `*`, The arguments to internationalize. | ||
**Returns**: `String`, Then i18ned string. | ||
**Example**: | ||
<h6>Phrase notation with default parser.</h6> | ||
```js | ||
// Assuming the locale === 'ja', | ||
// a basic phrase returns 'こんにちは' | ||
__('Hello'); | ||
// a basic phrase with sprintf returns 'Bob こんにちは' | ||
__('Hello %s', 'Bob'); | ||
// a basic phrase with interpolation returns 'Bob こんにちは' | ||
__('Hello {{name}}', {name:'Bob'}); | ||
``` | ||
<h6>Bracket notation with default parser.</h6> | ||
```js | ||
// Assuming the locale === 'ja', | ||
// a basic bracket phrase returns 'おっす' | ||
__('[Hello].informal'); | ||
// a basic bracket phrase with sprintf returns 'Bob おっす' | ||
__('[Hello %].informal', 'Bob'); | ||
// a basic bracket phrase with interpolation returns 'Bob おっす' | ||
__('[Hello {{name}}].informal', {name:'Bob'}); | ||
``` | ||
<h6>Dot notation with default parser.</h6> | ||
```js | ||
// Assuming the locale === 'ja', | ||
// a basic dot phrase returns 'おっす' | ||
__('greeting.hello.informal'); | ||
// a basic dot phrase with sprintf returns 'Bob おっす' | ||
__('greeting.hello.person.informal', 'Bob'); | ||
//a basic dot phrase with interpolation returns 'Bob おっす' | ||
__('greeting.hello.person.informal', {name:'Bob'}); | ||
``` | ||
<h6>All notations with Message Format.</h6> | ||
Note: The format parser uses Yahoo's [intl-messageformat](https://github.com/yahoo/intl-messageformat). | ||
```js | ||
// Assuming the locale === 'en-us', | ||
// a basic phrase with message formatting | ||
// returns "You have 1,000 photos." | ||
__('You have {n, plural, =0 {no photos.}=1 {one photo.}other {# photos.}}', { n: 1000 }); | ||
// a basic bracket phrase with message formatting | ||
// returns "You have 1,000 photos since Jan 1, 2015 9:33:04 AM." | ||
__('[You have {n, plural, =0 {no photos.}=1 {one photo.}other {# photos.}}].since.date', | ||
{ n:4000, d:new Date() }); | ||
// a basic dot phrase with message formatting | ||
// returns "You have 1,000 photos since Jan 1, 2015 9:33:04 AM." | ||
__('pictures.since.date', { n:4000, d:new Date() }); | ||
``` | ||
### language(id) | ||
Returns the name of the current locale. | ||
**Parameters** | ||
**id**: `string`, The locale to change. | ||
**Returns**: `String`, Then i18ned string. | ||
**Example**: | ||
<h6>Get the current language.</h6> | ||
```js | ||
// assuming locale === 'en-us' | ||
// returns 'American English' | ||
__.languages(); | ||
``` | ||
<h6>Get the current language in another locale. </h6> | ||
```js | ||
// assuming locale === 'en-us' | ||
// returns 'English' | ||
__.language('en'); | ||
// returns 'Japanese' | ||
__.language('ja'); | ||
``` | ||
**Example**: | ||
<h6>Get the current language.</h6> | ||
```js | ||
// assuming locale === 'en-us' | ||
// returns 'American English' | ||
__.languages(); | ||
``` | ||
<h6>Get the current language in another locale. </h6> | ||
```js | ||
// assuming locale === 'en-us' | ||
// returns 'English' | ||
__.language('en'); | ||
// returns 'Japanese' | ||
__.language('ja'); | ||
``` | ||
### languages(arg, supported) | ||
Returns the names of the supported locale. | ||
**Parameters** | ||
**arg**: `String | Array`, The locale to change or the supported locales. | ||
**supported**: `Array`, The supported locales. | ||
**Returns**: `String`, Then i18ned string. | ||
**Example**: | ||
<h6>Get the supported languages.</h6> | ||
```js | ||
// Assuming locale === 'en-us' | ||
// returns ['American English', 'Japanese'] | ||
__.lanugages(); | ||
``` | ||
<h6>Get the current languages in another locale. </h6> | ||
```js | ||
// Assuming locale === 'en-us' | ||
// returns ['アメリカ英語', '日本語'] | ||
__.languages('ja'); | ||
``` | ||
<h6>Override the supported locales.</h6> | ||
```js | ||
// Assuming locale === 'en-us' | ||
// returns ['English', 'Japanese'] | ||
__.languages(['en', 'ja']); | ||
``` | ||
<h6>Override the supported locales and get the languages in another locale.</h6> | ||
```js | ||
// Assuming locale === 'en-us' | ||
// returns ['英語', '日本語'] | ||
__.languages('ja', ['en', 'ja']); | ||
``` | ||
### locales(locale) | ||
Sets or gets the locale. | ||
**Parameters** | ||
**locale**: `String`, The locale to set or get. | ||
**Returns**: `String`, The locale. | ||
**Example**: | ||
<h6>Get the current locale.</h6> | ||
```js | ||
// Assuming locale === 'en-us' | ||
// returns 'en-us' | ||
__.locale() | ||
``` | ||
<h6>Set the locale.</h6> | ||
```js | ||
// Asumming locale === 'en-us' | ||
// sets and returns 'ja' | ||
__.locale('ja') | ||
``` | ||
### catalog(locale) | ||
Returns the catalog. | ||
**Parameters** | ||
**locale**: `String`, The locale to get. | ||
**Returns**: `Object`, The catalog. | ||
**Example**: | ||
<h6>Get the entire catalog.</h6> | ||
```js | ||
// Returns the entire catalog | ||
__.catalog() | ||
``` | ||
<h6>Set the locale and return the catalog.</h6> | ||
```js | ||
// Sets and returns the specified catalog | ||
__.catalog('en-us') | ||
``` | ||
### l10n(locale) | ||
Localizes date, time and numbers. | ||
See [Tokei](https://github.com/iwatakeshi/tokei) for documentation. | ||
Note: You can change ID for l10n. See Options. | ||
**Parameters** | ||
**locale**: `String`, The locale to override. | ||
**Returns**: `Tokei`, The context of Tokei. | ||
* * * | ||
## Reserved words | ||
@@ -317,0 +76,0 @@ |
/* global describe, it*/ | ||
var assert = require('chai').assert; | ||
var core = require('gengojs-core'); | ||
var api = require('../'); | ||
var api = require('../src/'); | ||
describe('API (plugin)', function() { | ||
@@ -10,5 +11,5 @@ 'use strict'; | ||
var gengo = core({}, api()); | ||
assert.isDefined(gengo.plugins.apis[0]); | ||
assert.isDefined(gengo.plugins.api); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
46559
13
755
4
15
93
1
+ Addedgengojs-debug@^2.0.4
+ Addedamdefine@1.0.1(transitive)
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedgengojs-debug@2.0.4(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedsource-map@0.1.32(transitive)
+ Addedsource-map-support@0.3.3(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)