Commit 76fe1228 authored by Dylan R. E. Moonfire's avatar Dylan R. E. Moonfire
Browse files

Initial commit.

parents
# EditorConfig is awesome: http://EditorConfig.org
# top-most EditorConfig file
root = true
[*]
charset = utf-8
end_of_line = crlf
indent_brace_style = K&R
indent_size = 4
indent_style = space
insert_final_newline = true
max_line_length = 80
tab_width = 4
trim_trailing_whitespace = true
curly_bracket_next_line = false
[*.{js,ts}]
quote_type = double
[*.json]
indent_size = 2
tab_width = 2
[*.yaml]
trim_trailing_whitespace = false
indent_style = space
*~
.tscache
/.ntvs_analysis.dat
/.ntvs_analysis.dat.tmp
.baseDir.ts
npm-debug.log
*.tgz
src/*.js
src/*.js.map
src/*.d.ts
lib/
es6/
amd/
umd/
dist/
lib/
commonjs/
spec/*.js
spec/*.js.map
spec/*.d.ts
node_modules/
typings/
*~
*.tgz
TODO.markdown
.editorconfig
.jsbeautifyrc
src/
lib/spec/
typings/
tsd.json
The MIT License (MIT)
Copyright (c) 2015 Dylan R. E. Moonfire
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# MfGames Culture (Javascript)
A library for handling arbitrary cultures and calendars for fantasy and sci-fi.
## Installation
Download node at [nodejs.org](http://nodejs.org) and install it, if you haven't already.
```sh
npm install mfgames-culture-javascript --save
```
## Tests
```sh
npm test
```
## License
MIT
---
title: General To Do Items
---
* Implement the duodecimal calendar.
* Handling two calendars in a single culture.
* Handle formatting two calendars at once.
* Parsing calendar strings into point.
* Calculating partial points into proper Julian Dates.
{
"name": "mfgames-culture-js",
"version": "0.2.0",
"description": "A library for handling arbitrary cultures and calendars for fantasy and sci-fi.",
"license": "MIT",
"author": {
"name": "Dylan R. E. Moonfire",
"email": "d.moonfire@mfgames.com",
"url": "https://d.moonfire.us"
},
"keywords": [
"fantasy",
"sci-fi",
"calendar"
],
"scripts": {
"test": "mocha lib/tests",
"lint": "tslint src/*.ts",
"build": "tsc -p src/tsconfig.json",
"prebuild": "npm run lint"
},
"homepage": "https://mfgames.com/mfgames-culture",
"repository": {
"type": "git",
"url": "https://github.com/dmoonfire/mfgames-culture-js"
},
"bugs": {
"url": "https://github.com/dmoonfire/mfgames-culture-js/issues"
},
"devDependencies": {
"@types/mocha": "^2.2.41",
"@types/node": "^7.0.18",
"expect": "^1.20.2",
"jasmine-node": "^1.14.5",
"mfgames-culture-data": "^1.0.2",
"mocha": "^3.3.0",
"tslint": "^5.2.0",
"typescript": "^1.6.2"
},
"dependencies": {
"@types/big.js": "0.0.31",
"@types/bluebird": "^3.5.3",
"big.js": "^3.1.3",
"bluebird": "^3.5.0"
},
"main": "lib/index.js"
}
export * from "./interfaces";
export * from "./process";
export * from "./providers";
/**
* Defines the JSON signature for cycle length calculations. This is used by
* both the repeat and sequence cycles.
*
* @interface
*/
export interface CalendarLengthData {
/**
* The amount to adjust a *cycle index* when this length is applied. This
* must be a whole number (1 or higher integer).
*/
count: number;
/**
* The amount to modify the Julian Day Number when this length is valid for
* a cycle calculation. Typically `julian` or `single` is required to
* calculate the length of a cycle.
*/
julian?: number;
/**
* A series of length elements that have various calculations. Only one
* length within the single is selected, the first one that is valid for
* the current state.
*/
single?: Array<CalendarLengthData>;
/**
* The reference to a calculated cycle id. This is used with the operation
* and value properties to determine if this length is valid for the current
* state. Only cycles that were already calculated, such as parent cycles
* and ones earlier in the sequence, can be referenced.
*
* This is only required with conditional lengths.
*/
ref?: string;
/**
* The operation used to determine if a length is valid. This is used with
* ref and value. The only values can be "div" and "mod".
*
* This is only required with conditional lengths.
*/
operation?: string;
/**
* The value used for the operations and ref properties. The value is
* determined by the operation, which requires an integer value.
*
* This is only required with conditional lengths.
*/
value?: number;
/**
* The value to determine if cycle length calculations should stop looping
* if the length returned a valid value.
*/
stopIfValid: boolean;
}
/**
* Defines the JSON data for a calendar cycle.
*
* @interface
*/
export interface CalendarCycleData {
/**
* The identifier for the cycle. This must be unique not only within a
* single calendar but also all other combined calendars used by a given
* culture.
*/
id: string;
type: string;
cycles: Array<CalendarCycleData>;
lengths?: Array<CalendarLengthData>;
ref?: string;
value?: number;
operation?: string;
offset?: number;
partialDaysOnly?: boolean;
}
/**
* Base class for all top-level culture components.
*/
export interface ComponentData {
id: string;
version: number;
type: string;
}
export interface CultureTemporalFormatElementData {
ref?: string;
constant?: string;
default?: any;
minDigits?: number;
maxDigits?: number;
offset?: number;
prefix?: string;
suffix?: string;
lookup?: boolean;
maxValue?: number;
minValue?: number;
parseRef?: string;
/**
* An offset for the resulting Julian date while parsing to avoid doubling
* changes while using two different calendars.
*/
parseJulianOffset?: number|string;
}
export interface CultureTemporalData {
calendars: Array<string>;
instantFormats: { [id: string]: Array<CultureTemporalFormatElementData> };
}
/**
* The metadata and top-level information for calendar
*/
export interface CalendarData extends ComponentData {
cycles?: Array<CalendarCycleData>;
}
export interface CultureData extends ComponentData {
temporal?: CultureTemporalData;
lookups: { [id: string]: string };
}
export interface CultureDataProvider {
getCalendarData(id: string): Promise<CalendarData>;
getCultureData(id: string): Promise<CultureData>;
}
This diff is collapsed.
import * as Promise from "bluebird";
import { CalendarData, CalendarCycleData, CalendarLengthData, CultureData, CultureTemporalData, CultureTemporalFormatElementData, CultureDataProvider, ComponentData } from "./interfaces";
export class ArrayCultureDataProvider implements CultureDataProvider {
constructor(components: Array<ComponentData>) {
this.components = components;
}
private components: Array<ComponentData>;
public getCalendarData(id: string): Promise<CalendarData> {
var that = this;
return new Promise<CalendarData>(function(resolve, error) {
var calendarData: CalendarData = that.getComponent(id);
resolve(calendarData);
});
}
public getCultureData(id: string): Promise<CultureData> {
var that = this;
return new Promise<CultureData>(function(resolve, error) {
var cultureData: CultureData = that.getComponent(id);
resolve(cultureData);
});
}
private getComponent(id: string): any {
for (var component of this.components) {
if (component.id === id) {
return component;
}
}
throw new Error("Cannot find component ID: '" + id + "'.");
}
}
export class HashCultureDataProvider implements CultureDataProvider {
constructor(components: any) {
this.components = components;
}
private components: any;
public getCalendarData(id: string): Promise<CalendarData> {
var that = this;
return new Promise<CalendarData>(function(resolve, error) {
var calendarData: CalendarData = that.getComponent(id);
resolve(calendarData);
});
}
public getCultureData(id: string): Promise<CultureData> {
var that = this;
return new Promise<CultureData>(function(resolve, error) {
var cultureData: CultureData = that.getComponent(id);
resolve(cultureData);
});
}
private getComponent(id: string): any {
if (id in this.components) {
return this.components[id];
}
throw new Error("Cannot find component ID: '" + id + "'.");
}
}
let expect = require("expect");
import { Calendar } from "../index";
import { getJulian, getCalendar } from "./helper";
describe("duodecimal calendar", function() {
it("can get components for 00:00:00", function(done) {
getCalendar("duodecimal").then(
function(cal: Calendar) {
try {
var julian = 0.5 + 0 / 24 + 0 / 1440 + 0 / 86400;
var point = cal.getInstant(julian);
expect(point).toEqual({
julian: 0.5,
hour24: 0,
hour12: 0,
hourMinute: 0,
//dayMinute: 0,
minuteSecond: 0,
meridiem: 0
});
done();
} catch (exception) {
done(exception);
}
},
function(error) {
done("There was an error while running: " + error);
});
});
xit("can get components for 12:00:00", function(done) {
getCalendar("duodecimal").then(
function(cal: Calendar) {
try {
var julian = 0.5 + 12 / 24 + 0 / 1440 + 0 / 86400;
var point = cal.getInstant(julian);
expect(point).toEqual({
julian: 1,
hour24: 12,
hour12: 0,
hourMinute: 0,
//dayMinute: 43200,
minuteSecond: 0,
meridiem: 1
});
done();
} catch (exception) {
done(exception);
}
},
function(error) {
done("There was an error while running: " + error);
});
});
});
const expect = require("expect");
describe("environment", function() {
it("verify test framework", function () {
expect("test").toEqual("test");
});
});
const expect = require("expect");
import { Calendar, Culture } from "../index";
import { getJulian, getCalendar, getCulture } from "./helper";
describe("gregorian/decimal format", function() {
it("can format 2001-01-01 00:00:00 as YYYY-MM-DD HH:mm:ss", function(done) {
getCulture("en-US").then(
function(culture: Culture) {
try {
var julian = getJulian(2001, 1, 1, 0, 0, 0);
var calendar = culture.calendar;
expect(calendar).toExist();
var instant = calendar.getInstant(julian);
var formattedInstant = culture.formatInstant(instant, "YYYY-MM-DD HH:mm:ss");
expect(formattedInstant).toEqual("2001-01-01 00:00:00");
done();
} catch (exception) {
done(exception);
}
},
function(error) { done(error); });
});
it("can format 2001-01-01 01:02:03 as YYYY-MM-DD HH:mm:ss", function(done) {
getCulture("en-US").then(
function(culture: Culture) {
try {
var julian = getJulian(2001, 1, 1, 1, 2, 3);
var calendar = culture.calendar;
expect(calendar).toExist();
var instant = calendar.getInstant(julian);
var formattedInstant = culture.formatInstant(instant, "YYYY-MM-DD HH:mm:ss");
expect(formattedInstant).toEqual("2001-01-01 01:02:03");
done();
} catch (exception) {
done(exception);
}
},
function(error) { done(error); });
});
it("can format 2001-01-01 13:12:14 as YYYY-MM-DD HH:mm:ss", function(done) {
getCulture("en-US").then(
function(culture: Culture) {
try {
var julian = getJulian(2001, 1, 1, 13, 12, 14);
var calendar = culture.calendar;
expect(calendar).toExist();
var instant = calendar.getInstant(julian);
var formattedInstant = culture.formatInstant(instant, "YYYY-MM-DD HH:mm:ss");
expect(formattedInstant).toEqual("2001-01-01 13:12:14");
done();
} catch (exception) {
done(exception);
}
},
function(error) { done(error); });
});
xit("can format 2001-01-01 13:12:14 as MM/DD/YYYY h:mm:ss tt", function(done) {
getCulture("en-US").then(
function(culture: Culture) {
try {
var julian = getJulian(2001, 1, 1, 13, 12, 14);
var calendar = culture.calendar;
expect(calendar).toExist();
var instant = calendar.getInstant(julian);
var formattedInstant = culture.formatInstant(instant, "MM/DD/YYYY h:mm:ss tt");
expect(formattedInstant).toEqual("01/01/2001 1:12:14 pm");
done();
} catch (exception) {
done(exception);
}
},
function(error) { done(error); });
});
});
const expect = require("expect");
import { Calendar, Culture } from "../index";
import { getJulian, getCalendar, getCulture } from "./helper";
describe("gregorian format", function() {
it("can't format 2001-01-01 as INVALID", function(done) {
getCulture("en-US").then(
function(culture: Culture) {
try {
var julian = getJulian(2001, 1, 1);
var calendar = culture.calendar;
var instant = calendar.getInstant(julian);
var formattedInstant = culture.formatInstant(instant, "INVALID");
done(new Error("Incorrectly formatted a date as INVALID."));
} catch (exception) {
done();
}
},
function(error) { done(error); });
});
it("can format 2001-01-01 as MM/DD/YYYY", function(done) {
getCulture("en-US").then(
function(culture: Culture) {
try {
var julian = getJulian(2001, 1, 1);
var calendar = culture.calendar;
expect(calendar).toExist();
var instant = calendar.getInstant(julian);
var formattedInstant = culture.formatInstant(instant, "MM/DD/YYYY");
expect(formattedInstant).toEqual("01/01/2001");
done();
} catch (exception) {
done(exception);
}
},
function(error) { done(error); });
});
it("can format 2001-01-01 as MMM DD, YY", function(done) {
getCulture("en-US").then(
function(culture: Culture) {
try {
var julian = getJulian(2001, 1, 1);
var calendar = culture.calendar;
expect(calendar).toExist();
var instant = calendar.getInstant(julian);
var formattedInstant = culture.formatInstant(instant, "MMM DD, YY");
expect(formattedInstant).toEqual("Jan 01, 01");
done();
} catch (exception) {
done(exception);
}
},
function(error) { done(error); });
});
});
const expect = require("expect");
import { Calendar } from "../index";
import { getJulian, getCalendar } from "./helper";
describe("gregorian calendar", function() {
it("can get components for 2001-01-01", function(done) {
getCalendar("gregorian").then(
function(cal: Calendar) {
try {
var julian = getJulian(2001, 1, 1);
var point = cal.getInstant(julian);
expect(point).toEqual({
julian: 2451910.5,
year: 2001,
century: 20,
millenniumCentury: 0,
decade: 200,
centuryDecade: 0,
centuryYear: 1,
decadeYear: 1,
millennium: 2,