Skip to content

Commit

Permalink
optimize locale
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Jan 22, 2015
1 parent 8934cfe commit 52891b2
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 211 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -19,4 +19,6 @@ Thumbs.db
.*proj
.svn/
*.swp
out/
out/
_site/
spm_modules/
4 changes: 3 additions & 1 deletion .npmignore
Expand Up @@ -18,4 +18,6 @@ Thumbs.db
.project
.*proj
.svn/
*.swp
*.swp
_site/
spm_modules/
1 change: 1 addition & 0 deletions README.md
@@ -1,4 +1,5 @@
# gregorian-calendar-format
---

format [gregorian-calendar](https://github.com/kissyteam/gregorian-calendar) instance

Expand Down
6 changes: 0 additions & 6 deletions demo/index.html

This file was deleted.

14 changes: 14 additions & 0 deletions examples/index.md
@@ -0,0 +1,14 @@
# gregorian-calendar-format@2.x
---


````js
var DateTimeFormat = require('../');
var GregorianCalendar = require('gregorian-calendar');
var gregorianCalendar = new GregorianCalendar(2013,
GregorianCalendar.JULY, 9);
var df = new DateTimeFormat('yyyy-MM-dd');
console.log(df.format(gregorianCalendar));
df = new DateTimeFormat('yy-MM-dd');
console.log(df.format(gregorianCalendar));
````
36 changes: 0 additions & 36 deletions gulpfile.js

This file was deleted.

30 changes: 22 additions & 8 deletions lib/gregorian-calendar-format.js
Expand Up @@ -354,11 +354,8 @@ function zeroPaddingNumber(value, minDigits, maxDigits, buffer) {
* @param {Number} timeZoneOffset time zone offset by minutes
*/
function DateTimeFormat(pattern, locale, timeZoneOffset) {
this.locale = locale || GregorianCalendar.defaultLocale;
this.locale = locale;
this.pattern = compile(pattern);
if (typeof timeZoneOffset === 'undefined') {
timeZoneOffset = this.locale.timezoneOffset;
}
this.timezoneOffset = timeZoneOffset;
}

Expand Down Expand Up @@ -630,6 +627,23 @@ function parseField(calendar, dateStr, startIndex, field, count, locale, obeyCou
}

mix(DateTimeFormat.prototype, {
getLocale: function (calendar) {
if (this.locale) {
return this.locale;
}
if (calendar) {
return calendar.locale;
}
return GregorianCalendar.defaultLocale;
},

getTimezoneOffset: function (calendar) {
if (typeof this.timezoneOffset !== 'undefined') {
return this.timezoneOffset;
}
return this.getLocale(calendar).timezoneOffset;
},

/**
* format a GregorianDate instance according to specified pattern
* @param {Date.Gregorian} calendar GregorianDate instance
Expand All @@ -641,7 +655,7 @@ mix(DateTimeFormat.prototype, {
} else {
var time = calendar.getTime();
calendar = /**@type {Date.Gregorian}
@ignore*/new GregorianCalendar(this.timezoneOffset, this.locale);
@ignore*/new GregorianCalendar(this.getTimezoneOffset(), this.getLocale());
calendar.setTime(time);
}
var i,
Expand All @@ -653,7 +667,7 @@ mix(DateTimeFormat.prototype, {
if (comp.text) {
ret.push(comp.text);
} else if ('field' in comp) {
ret.push(formatField(comp.field, comp.count, this.locale, calendar));
ret.push(formatField(comp.field, comp.count, this.getLocale(calendar), calendar));
}
}
return ret.join('');
Expand All @@ -666,7 +680,7 @@ mix(DateTimeFormat.prototype, {
*/
parse: function (dateStr) {
var calendar = /**@type {Date.Gregorian}
@ignore*/new GregorianCalendar(this.timezoneOffset, this.locale),
@ignore*/new GregorianCalendar(this.getTimezoneOffset(), this.getLocale()),
i,
j,
tmp = {},
Expand Down Expand Up @@ -713,7 +727,7 @@ mix(DateTimeFormat.prototype, {
startIndex,
comp.field,
comp.count,
this.locale,
this.getLocale(calendar),
obeyCount,
tmp);
if (startIndex === oldStartIndex) {
Expand Down
48 changes: 10 additions & 38 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "gregorian-calendar-format",
"version": "2.0.2",
"version": "2.1.0",
"author": {
"name": "yiminghe",
"email": "yiminghe@gmail.com"
Expand All @@ -14,40 +14,10 @@
"url": "http://github.com/kissyteam/gregorian-calendar-format"
},
"devDependencies": {
"aggregate-bower": "^1.0.2",
"async": "^0.9.0",
"auto-deps": "^1.0.3",
"expect.js": "^0.3.1",
"gulp": "^3.8.7",
"gulp-clean": "^0.3.1",
"gulp-filter": "^1.0.0",
"gulp-footer": "^1.0.5",
"gulp-header": "^1.0.5",
"gulp-jscs": "^1.1.0",
"gulp-jshint": "^1.8.4",
"gulp-kclean": "~0.0.15",
"gulp-modulex": "^1.0.0",
"gulp-rename": "^1.2.0",
"gulp-replace": "^0.4.0",
"gulp-uglify": "^0.3.1",
"gulp-util": "^3.0.0",
"gulp-wrapper": "^0.1.5",
"jshint": "^2.5.5",
"jshint-stylish": "^0.4.0",
"koa": "^0.11.0",
"koa-body": "^0.2.0",
"koa-modularize": "^3.0.3",
"koa-mount": "^1.3.0",
"koa-node-jscover": "^1.1.1",
"koa-serve-index": "^1.0.1",
"koa-static": "^1.4.7",
"mocha": "^1.21.4",
"modulex": "^1.7.4",
"node-jscover": "^0.6.8",
"node-jscover-coveralls": "^1.0.10",
"node-jscover-handler": "^1.0.3",
"precommit-hook": "^1.0.7",
"through2": "^0.6.2"
"rc-server": "~1.5.9",
"rc-tools": "~1.1.5"
},
"precommit": [
"lint"
Expand All @@ -56,12 +26,14 @@
"port": 8012
},
"scripts": {
"start": "node --harmony server",
"history": "rc-tools run history",
"start": "node --harmony node_modules/.bin/rc-server",
"publish": "spm doc publish && spm publish && rc-tools run tag",
"lint": "rc-tools run lint",
"test": "",
"publish": "gulp tag",
"lint": "gulp lint",
"browser-test": "mocha-phantomjs http://localhost:$npm_package_config_port/tests/browser/runner.html",
"browser-test-cover": "mocha-phantomjs -R node_modules/node-jscover/lib/reporters/mocha/console http://localhost:$npm_package_config_port/tests/browser/runner.html?coverage"
"saucelabs": "rc-tools run saucelabs",
"browser-test": "rc-tools run browser-test",
"browser-test-cover": "rc-tools run browser-test-cover"
},
"dependencies": {
"gregorian-calendar": "^2.0.2"
Expand Down
45 changes: 0 additions & 45 deletions server.js

This file was deleted.

75 changes: 0 additions & 75 deletions tests/browser/runner.html

This file was deleted.

3 changes: 2 additions & 1 deletion tests/browser/specs/index.js → tests/index.spec.js
Expand Up @@ -3,10 +3,11 @@
* @author yiminghe@gmail.com
*/

var DateTimeFormat = require('../../../');
var DateTimeFormat = require('../');
var GregorianCalendar = require('gregorian-calendar');
var zhCn = require('gregorian-calendar/lib/locale/zh-cn');
var Style = DateTimeFormat.Style;
var expect = require('expect.js');

describe('DateTimeFormat', function () {
describe('format', function () {
Expand Down
1 change: 1 addition & 0 deletions tests/runner.html
@@ -0,0 +1 @@
stub

0 comments on commit 52891b2

Please sign in to comment.