forked from jamesshore/quixote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.js
53 lines (44 loc) · 1.29 KB
/
value.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) 2014 Titanium I.T. LLC. All rights reserved. For license, see "README" or "LICENSE" file.
"use strict";
var ensure = require("../util/ensure.js");
var oop = require("../util/oop.js");
var shim = require("../util/shim.js");
var Me = module.exports = function Value() {};
Me.extend = oop.extendFn(Me);
oop.makeAbstract(Me, [
"diff",
"toString",
"compatibility"
]);
Me.safe = function safe(fn) {
return function() {
ensureCompatibility(this, this.compatibility(), arguments);
return fn.apply(this, arguments);
};
};
Me.prototype.value = function value() {
ensure.signature(arguments, []);
return this;
};
Me.prototype.equals = function equals(that) {
return this.diff(that) === "";
};
function ensureCompatibility(self, compatible, args) {
var arg;
for (var i = 0; i < args.length; i++) { // args is not an Array, can't use forEach
arg = args[i];
checkOneArg(self, compatible, arg);
}
}
function checkOneArg(self, compatible, arg) {
var type = typeof arg;
if (arg === null) type = "null";
if (type !== "object") throwError(type);
for (var i = 0; i < compatible.length; i++) {
if (arg instanceof compatible[i]) return;
}
throwError(oop.instanceName(arg));
function throwError(type) {
throw new Error(oop.instanceName(self) + " isn't compatible with " + type);
}
}