Skip to content

Commit c0eaddb

Browse files
alexanderGugelmichaelobriena
authored andcommitted
refact: No longer use class for Path
Path is simply a collection of static methods. Making it a class doesn't have any advantages IMO, especially since a singleton object is being exported anyways. I think it's more intuitive to see it as a collection of useful methods. This change doesn't break existing APIs.
1 parent 5f2cfcb commit c0eaddb

File tree

1 file changed

+135
-135
lines changed

1 file changed

+135
-135
lines changed

core/Path.js

Lines changed: 135 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/**
22
* The MIT License (MIT)
3-
*
3+
*
44
* Copyright (c) 2015 Famous Industries Inc.
5-
*
5+
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
88
* in the Software without restriction, including without limitation the rights
99
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
* copies of the Software, and to permit persons to whom the Software is
1111
* furnished to do so, subject to the following conditions:
12-
*
12+
*
1313
* The above copyright notice and this permission notice shall be included in
1414
* all copies or substantial portions of the Software.
15-
*
15+
*
1616
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -27,146 +27,146 @@
2727
/**
2828
* A collection of utilities for handling paths.
2929
*
30-
* @class
30+
* @namespace
3131
*/
32-
function PathUtils () {
33-
}
32+
var Path = {
3433

35-
/**
36-
* determines if the passed in path has a trailing slash. Paths of the form
37-
* 'body/0/1/' return true, while paths of the form 'body/0/1' return false.
38-
*
39-
* @method
40-
*
41-
* @param {String} path the path
42-
*
43-
* @return {Boolean} whether or not the path has a trailing slash
44-
*/
45-
PathUtils.prototype.hasTrailingSlash = function hasTrailingSlash (path) {
46-
return path[path.length - 1] === '/';
47-
};
34+
/**
35+
* determines if the passed in path has a trailing slash. Paths of the form
36+
* 'body/0/1/' return true, while paths of the form 'body/0/1' return false.
37+
*
38+
* @method
39+
*
40+
* @param {String} path the path
41+
*
42+
* @return {Boolean} whether or not the path has a trailing slash
43+
*/
44+
hasTrailingSlash: function hasTrailingSlash (path) {
45+
return path[path.length - 1] === '/';
46+
},
4847

49-
/**
50-
* Returns the depth in the tree this path represents. Essentially counts
51-
* the slashes ignoring a trailing slash.
52-
*
53-
* @method
54-
*
55-
* @param {String} path the path
56-
*
57-
* @return {Number} the depth in the tree that this path represents
58-
*/
59-
PathUtils.prototype.depth = function depth (path) {
60-
var count = 0;
61-
var length = path.length;
62-
var len = this.hasTrailingSlash(path) ? length - 1 : length;
63-
var i = 0;
64-
for (; i < len ; i++) count += path[i] === '/' ? 1 : 0;
65-
return count;
66-
};
48+
/**
49+
* Returns the depth in the tree this path represents. Essentially counts
50+
* the slashes ignoring a trailing slash.
51+
*
52+
* @method
53+
*
54+
* @param {String} path the path
55+
*
56+
* @return {Number} the depth in the tree that this path represents
57+
*/
58+
depth: function depth (path) {
59+
var count = 0;
60+
var length = path.length;
61+
var len = this.hasTrailingSlash(path) ? length - 1 : length;
62+
var i = 0;
63+
for (; i < len ; i++) count += path[i] === '/' ? 1 : 0;
64+
return count;
65+
},
6766

68-
/**
69-
* Gets the position of this path in relation to its siblings.
70-
*
71-
* @method
72-
*
73-
* @param {String} path the path
74-
*
75-
* @return {Number} the index of this path in relation to its siblings.
76-
*/
77-
PathUtils.prototype.index = function index (path) {
78-
var length = path.length;
79-
var len = this.hasTrailingSlash(path) ? length - 1 : length;
80-
while (len--) if (path[len] === '/') break;
81-
var result = parseInt(path.substring(len + 1));
82-
return isNaN(result) ? 0 : result;
83-
};
67+
/**
68+
* Gets the position of this path in relation to its siblings.
69+
*
70+
* @method
71+
*
72+
* @param {String} path the path
73+
*
74+
* @return {Number} the index of this path in relation to its siblings.
75+
*/
76+
index: function index (path) {
77+
var length = path.length;
78+
var len = this.hasTrailingSlash(path) ? length - 1 : length;
79+
while (len--) if (path[len] === '/') break;
80+
var result = parseInt(path.substring(len + 1));
81+
return isNaN(result) ? 0 : result;
82+
},
8483

85-
/**
86-
* Gets the position of the path at a particular breadth in relationship
87-
* to its siblings
88-
*
89-
* @method
90-
*
91-
* @param {String} path the path
92-
* @param {Number} depth the breadth at which to find the index
93-
*
94-
* @return {Number} index at the particular depth
95-
*/
96-
PathUtils.prototype.indexAtDepth = function indexAtDepth (path, depth) {
97-
var i = 0;
98-
var len = path.length;
99-
var index = 0;
100-
for (; i < len ; i++) {
101-
if (path[i] === '/') index++;
102-
if (index === depth) {
103-
path = path.substring(i ? i + 1 : i);
104-
index = path.indexOf('/');
105-
path = index === -1 ? path : path.substring(0, index);
106-
index = parseInt(path);
107-
return isNaN(index) ? path : index;
84+
/**
85+
* Gets the position of the path at a particular breadth in relationship
86+
* to its siblings
87+
*
88+
* @method
89+
*
90+
* @param {String} path the path
91+
* @param {Number} depth the breadth at which to find the index
92+
*
93+
* @return {Number} index at the particular depth
94+
*/
95+
indexAtDepth: function indexAtDepth (path, depth) {
96+
var i = 0;
97+
var len = path.length;
98+
var index = 0;
99+
for (; i < len ; i++) {
100+
if (path[i] === '/') index++;
101+
if (index === depth) {
102+
path = path.substring(i ? i + 1 : i);
103+
index = path.indexOf('/');
104+
path = index === -1 ? path : path.substring(0, index);
105+
index = parseInt(path);
106+
return isNaN(index) ? path : index;
107+
}
108108
}
109-
}
110-
};
109+
},
111110

112-
/**
113-
* returns the path of the passed in path's parent.
114-
*
115-
* @method
116-
*
117-
* @param {String} path the path
118-
*
119-
* @return {String} the path of the passed in path's parent
120-
*/
121-
PathUtils.prototype.parent = function parent (path) {
122-
return path.substring(0, path.lastIndexOf('/', path.length - 2));
123-
};
111+
/**
112+
* returns the path of the passed in path's parent.
113+
*
114+
* @method
115+
*
116+
* @param {String} path the path
117+
*
118+
* @return {String} the path of the passed in path's parent
119+
*/
120+
parent: function parent (path) {
121+
return path.substring(0, path.lastIndexOf('/', path.length - 2));
122+
},
124123

125-
/**
126-
* Determines whether or not the first argument path is the direct child
127-
* of the second argument path.
128-
*
129-
* @method
130-
*
131-
* @param {String} child the path that may be a child
132-
* @param {String} parent the path that may be a parent
133-
*
134-
* @return {Boolean} whether or not the first argument path is a child of the second argument path
135-
*/
136-
PathUtils.prototype.isChildOf = function isChildOf (child, parent) {
137-
return this.isDescendentOf(child, parent) && this.depth(child) === this.depth(parent) + 1;
138-
};
124+
/**
125+
* Determines whether or not the first argument path is the direct child
126+
* of the second argument path.
127+
*
128+
* @method
129+
*
130+
* @param {String} child the path that may be a child
131+
* @param {String} parent the path that may be a parent
132+
*
133+
* @return {Boolean} whether or not the first argument path is a child of the second argument path
134+
*/
135+
isChildOf: function isChildOf (child, parent) {
136+
return this.isDescendentOf(child, parent) && this.depth(child) === this.depth(parent) + 1;
137+
},
139138

140-
/**
141-
* Returns true if the first argument path is a descendent of the second argument path.
142-
*
143-
* @method
144-
*
145-
* @param {String} child potential descendent path
146-
* @param {String} parent potential ancestor path
147-
*
148-
* @return {Boolean} whether or not the path is a descendent
149-
*/
150-
PathUtils.prototype.isDescendentOf = function isDescendentOf(child, parent) {
151-
if (child === parent) return false;
152-
child = this.hasTrailingSlash(child) ? child : child + '/';
153-
parent = this.hasTrailingSlash(parent) ? parent : parent + '/';
154-
return this.depth(parent) < this.depth(child) && child.indexOf(parent) === 0;
155-
};
139+
/**
140+
* Returns true if the first argument path is a descendent of the second argument path.
141+
*
142+
* @method
143+
*
144+
* @param {String} child potential descendent path
145+
* @param {String} parent potential ancestor path
146+
*
147+
* @return {Boolean} whether or not the path is a descendent
148+
*/
149+
isDescendentOf: function isDescendentOf(child, parent) {
150+
if (child === parent) return false;
151+
child = this.hasTrailingSlash(child) ? child : child + '/';
152+
parent = this.hasTrailingSlash(parent) ? parent : parent + '/';
153+
return this.depth(parent) < this.depth(child) && child.indexOf(parent) === 0;
154+
},
156155

157-
/**
158-
* returns the selector portion of the path.
159-
*
160-
* @method
161-
*
162-
* @param {String} path the path
163-
*
164-
* @return {String} the selector portion of the path.
165-
*/
166-
PathUtils.prototype.getSelector = function getSelector(path) {
167-
var index = path.indexOf('/');
168-
return index === -1 ? path : path.substring(0, index);
169-
};
156+
/**
157+
* returns the selector portion of the path.
158+
*
159+
* @method
160+
*
161+
* @param {String} path the path
162+
*
163+
* @return {String} the selector portion of the path.
164+
*/
165+
getSelector: function getSelector(path) {
166+
var index = path.indexOf('/');
167+
return index === -1 ? path : path.substring(0, index);
168+
}
170169

171-
module.exports = new PathUtils();
170+
};
172171

172+
module.exports = Path;

0 commit comments

Comments
 (0)