forked from taskrabbit/sudojs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudo.js
116 lines (116 loc) · 3.54 KB
/
sudo.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// #Sudo Namespace
var sudo = {
// Namespace for `Delegate` Class Objects used to delegate functionality
// from a `delegator`
//
// `namespace`
delegates: {},
// The sudo.extensions namespace holds the objects that are stand alone `modules` which
// can be `implemented` (mixed-in) in sudo Class Objects
//
// `namespace`
extensions: {},
// ###getPath
// Extract a value located at `path` relative to the passed in object
//
// `param` {String} `path`. The key in the form of a dot-delimited path.
// `param` {object} `obj`. An object literal to operate on.
//
// `returns` {*|undefined}. The value at keypath or undefined if not found.
getPath: function getPath(path, obj) {
var key, p;
p = path.split('.');
for (key; p.length && (key = p.shift());) {
if(!p.length) {
return obj[key];
} else {
obj = obj[key] || {};
}
}
return obj;
},
// ###inherit
// Inherit the prototype from a parent to a child.
// Set the childs constructor for subclasses of child.
// Subclasses of the library base classes will not
// want to use this function in *most* use-cases. Why? User Sudo Class Objects
// possess their own constructors and any call back to a `superclass` constructor
// will generally be looking for the library Object's constructor.
//
// `param` {function} `parent`
// `param` {function} `child`
inherit: function inherit(parent, child) {
child.prototype = Object.create(parent.prototype);
child.prototype.constructor = child;
},
// ###makeMeASandwich
// Notice there is no need to extrinsically instruct *how* to
// make the sandwich, just the elegant single command.
//
// `returns` {string}
makeMeASandwich: function makeMeASandwich() {return 'Okay.';},
// ###namespace
// Method for assuring a Namespace is defined.
//
// `param` {string} `path`. The path that leads to a blank Object.
namespace: function namespace(path) {
if (!this.getPath(path, window)) {
this.setPath(path, {}, window);
}
},
// ###premier
// The premier object takes precedence over all others so define it at the topmost level.
//
// `type` {Object}
premier: null,
// ###setPath
// Traverse the keypath and get each object
// (or make blank ones) eventually setting the value
// at the end of the path
//
// `param` {string} `path`. The path to traverse when setting a value.
// `param` {*} `value`. What to set.
// `param` {Object} `obj`. The object literal to operate on.
setPath: function setPath(path, value, obj) {
var p = path.split('.'), key;
for (key; p.length && (key = p.shift());) {
if(!p.length) {
obj[key] = value;
} else if (obj[key]) {
obj = obj[key];
} else {
obj = obj[key] = {};
}
}
},
// ####uid
// Some sudo Objects use a unique integer as a `tag` for identification.
// (Views for example). This ensures they are indeed unique.
uid: 0,
// ####unique
// An integer used as 'tags' by some sudo Objects as well
// as a unique string for views when needed
//
// `param` {string} prefix. Optional string identifier
unique: function unique(prefix) {
return prefix ? prefix + this.uid++ : this.uid++;
},
// ###unsetPath
// Remove a key:value pair from this object's data store
// located at <path>
//
// `param` {String} `path`
// `param` {Object} `obj` The object to operate on.
unsetPath: function unsetPath(path, obj) {
var p = path.split('.'), key;
for (key; p.length && (key = p.shift());) {
if(!p.length) {
delete obj[key];
} else {
// this can fail if a faulty path is passed.
// using getPath beforehand can prevent that
obj = obj[key];
}
}
}
};