Skip to content

Instantly share code, notes, and snippets.

@ongaeshi
Created April 23, 2011 16:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ongaeshi/938734 to your computer and use it in GitHub Desktop.
Save ongaeshi/938734 to your computer and use it in GitHub Desktop.
Firefox Add-on SDK date-ext.js and test.
//
// @brief
// @author ongaeshi
// @date 2011/04/24
const { Trait } = require("traits");
function formatNum(keta, num) {
var src = new String(num);
var cnt = keta - src.length;
if (cnt <= 0) return src;
while (cnt-- > 0) src = "0" + src; return src;
}
const DateExt = Trait.compose({
// public API
constructor: function DateExt(date) {
if (date == null)
this._date = new Date();
else
this._date = date;
},
get raw() this._date,
get year() this._date.getFullYear(),
get month() this._date.getMonth() + 1,
get day() this._date.getDate(),
get hour() this._date.getHours(),
get min() this._date.getMinutes(),
get Month() { return formatNum(2, this.month) },
get Day() { return formatNum(2, this.day) },
get Min() { return formatNum(2, this.min) },
dateStr1: function() {
return this.year + "/" + this.Month + "/" + this.Day + " " + this.hour + ":" + this.Min;
},
dateStr2: function() {
return this.year + "-" + this.Month + "-" + this.Day + " " + this.hour + ":" + this.Min;
},
// private API:
_date: null,
});
exports.DateExt = function(date) DateExt(date);
exports.DateExt.prototype = DateExt.prototype;
/* -*- tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
//
// @brief Firefox Add-on SDK traits test.
// @author ongaeshi
// @date 2011/04/23
// @url https://jetpack.mozillalabs.com/sdk/1.0b4/docs/packages/api-utils/docs/traits.html
const { Trait } = require('traits');
const List = Trait.compose({
// public API
// constructor: function List() this._list = [], // bug1
constructor: function List() {
this._list = [];
},
get length() this._list.length,
add: function add(item) this._list.push(item),
remove: function remove(item) {
let list = this._list;
let index = list.indexOf(item);
//if (0 <= index) list.slice(index, 1); // bug2
if (0 <= index) list.splice(index, 1);
},
// private API:
_list: null
});
const Range = List.resolve({
constructor: null,
add: '_add',
}).compose({
min: null,
max: null,
get list() this._list.slice(0),
constructor: function Range(min, max) {
this.min = min;
this.max = max;
this._list = [];
},
add: function(item) {
if (item <= this.max && item >= this.min)
this._add(item)
}
});
exports.test_test_run = function(test) {
test.pass("Traits Test .....");
let l1 = List();
test.assertEqual(l1 instanceof List, true); // true
test.assertEqual(l1.length, 0);
l1.add("test");
test.assertEqual(l1.length, 1);
let l2 = new List();
test.assertEqual(l2 instanceof List, true); // true
test.assertEqual(l2.length, 0);
l2.add("test");
l2.add("test2");
l2.add("test");
l2.add("test3");
test.assertEqual(l2.length, 4);
l2.remove("test");
l2.remove("test");
test.assertEqual(l2.length, 2);
let r = Range(0, 10);
test.assertEqual(r.min, 0); // 0
test.assertEqual(r.max, 10); // 10
test.assertEqual(r.length, 0); // 0;
r.add(5);
test.assertEqual(r.length, 1); // 1
r.add(12);
test.assertEqual(r.length, 1); // 1 (12 was not in a range)
// override test
{
// will compose trait with conflict property 'constructor'
var ConstructableList = List.compose({
constructor: function List() this._list = Array.slice(arguments)
});
// throws error with message 'Remaining conflicting property: constructor'
test.assertRaises(function(){ConstructableList(1, 2, 3)}, /conflicting property/);
// var ConstructableList = List.overridden({ // bug3
var ConstructableList = List.override({
constructor: function List() this._list = Array.slice(arguments)
});
test.assertEqual(ConstructableList(1, 2, 3).length, 3); // 3
}
// toString
{
const MyTrait = Trait.compose({
constructor: function MyTrait() {
// do your initialization here
}
});
test.assertEqual(MyTrait().toString(), "[object MyTrait]");
test.assertEqual(Range().toString(), "[object Range]");
}
// required
{
const Enumerable = Trait.compose({
list: Trait.required, // listプロパティが無いクラスとはcompose出来ない
forEach: function forEach(consumer) {
return this.list.forEach(consumer);
}
});
// Error: Missing required property: list
test.assertRaises(function() {Enumerable();}, /Missing required property/);
const EnumerableList = List.compose({
// get list: this._list.slice(0) // bug 5
get list() this._list.slice(0)
}, Enumerable);
let c2 = EnumerableList();
// bug 4 セミコロンが抜けてる
c2.add('test');
test.assertEqual(c2.length, 1); // 1
test.assertEqual(c2.list[0], 'test'); // 'test'
// c2.forEach(console.log); // > ×info: 'test' → ○info: test 0 test
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment