forked from kamicane/prime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.js
67 lines (45 loc) · 1.35 KB
/
shell.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
shell
*/"use strict"
var prime = require("./index"),
type = require("./type")
var slice = Array.prototype.slice
var ghost = prime({
constructor: function ghost(self){
this.valueOf = function(){
return self
}
this.toString = function(){
return self + ""
}
this.is = function(object){
return self === object
}
}
})
var shell = function(self){
if (self == null || self instanceof ghost) return self
var g = shell[type(self)]
return (g) ? new g(self) : self
}
var register = function(){
var g = prime({inherits: ghost})
return prime({
constructor: function(self){
return new g(self)
},
define: function(key, descriptor){
var method = descriptor.value
this[key] = function(self){
return (arguments.length > 1) ? method.apply(self, slice.call(arguments, 1)) : method.call(self)
}
g.prototype[key] = function(){
return shell(method.apply(this.valueOf(), arguments))
}
prime.define(this.prototype, key, descriptor)
return this
}
})
}
for (var types = "string,number,array,object,date,function,regexp".split(","), i = types.length; i--;) shell[types[i]] = register()
module.exports = shell