Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit d0b2c56

Browse files
committed
Update almond.js to 0.1.1
This version of almond includes changes for RequireJS 2.0
1 parent 098bf0f commit d0b2c56

File tree

1 file changed

+84
-47
lines changed

1 file changed

+84
-47
lines changed

vendor/assets/javascripts/almond.js

Lines changed: 84 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
/**
2-
* almond 0.0.3 Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
2+
* almond 0.1.1 Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
33
* Available via the MIT or new BSD license.
44
* see: http://github.com/jrburke/almond for details
55
*/
6-
/*jslint strict: false, plusplus: false */
6+
//Going sloppy to avoid 'use strict' string cost, but strict practices should
7+
//be followed.
8+
/*jslint sloppy: true */
79
/*global setTimeout: false */
810

911
var requirejs, require, define;
1012
(function (undef) {
11-
1213
var defined = {},
1314
waiting = {},
15+
config = {},
16+
defining = {},
1417
aps = [].slice,
1518
main, req;
1619

17-
if (typeof define === "function") {
18-
//If a define is already in play via another AMD loader,
19-
//do not overwrite.
20-
return;
21-
}
22-
2320
/**
2421
* Given a relative module name, like ./something, normalize it to
2522
* a real name that can be mapped to a path.
@@ -29,6 +26,11 @@ var requirejs, require, define;
2926
* @returns {String} normalized name
3027
*/
3128
function normalize(name, baseName) {
29+
var baseParts = baseName && baseName.split("/"),
30+
map = config.map,
31+
starMap = (map && map['*']) || {},
32+
nameParts, nameSegment, mapValue, foundMap, i, j, part;
33+
3234
//Adjust any relative paths.
3335
if (name && name.charAt(0) === ".") {
3436
//If have a base name, try to normalize against it,
@@ -40,13 +42,11 @@ var requirejs, require, define;
4042
//module. For instance, baseName of "one/two/three", maps to
4143
//"one/two/three.js", but we want the directory, "one/two" for
4244
//this normalization.
43-
baseName = baseName.split("/");
44-
baseName = baseName.slice(0, baseName.length - 1);
45+
baseParts = baseParts.slice(0, baseParts.length - 1);
4546

46-
name = baseName.concat(name.split("/"));
47+
name = baseParts.concat(name.split("/"));
4748

4849
//start trimDots
49-
var i, part;
5050
for (i = 0; (part = name[i]); i++) {
5151
if (part === ".") {
5252
name.splice(i, 1);
@@ -59,7 +59,7 @@ var requirejs, require, define;
5959
//no path mapping for a path starting with '..'.
6060
//This can still fail, but catches the most reasonable
6161
//uses of ..
62-
break;
62+
return true;
6363
} else if (i > 0) {
6464
name.splice(i - 1, 2);
6565
i -= 2;
@@ -71,6 +71,43 @@ var requirejs, require, define;
7171
name = name.join("/");
7272
}
7373
}
74+
75+
//Apply map config if available.
76+
if ((baseParts || starMap) && map) {
77+
nameParts = name.split('/');
78+
79+
for (i = nameParts.length; i > 0; i -= 1) {
80+
nameSegment = nameParts.slice(0, i).join("/");
81+
82+
if (baseParts) {
83+
//Find the longest baseName segment match in the config.
84+
//So, do joins on the biggest to smallest lengths of baseParts.
85+
for (j = baseParts.length; j > 0; j -= 1) {
86+
mapValue = map[baseParts.slice(0, j).join('/')];
87+
88+
//baseName segment has config, find if it has one for
89+
//this name.
90+
if (mapValue) {
91+
mapValue = mapValue[nameSegment];
92+
if (mapValue) {
93+
//Match, update name to the new value.
94+
foundMap = mapValue;
95+
break;
96+
}
97+
}
98+
}
99+
}
100+
101+
foundMap = foundMap || starMap[nameSegment];
102+
103+
if (foundMap) {
104+
nameParts.splice(0, i, foundMap);
105+
name = nameParts.join('/');
106+
break;
107+
}
108+
}
109+
}
110+
74111
return name;
75112
}
76113

@@ -99,8 +136,13 @@ var requirejs, require, define;
99136
if (waiting.hasOwnProperty(name)) {
100137
var args = waiting[name];
101138
delete waiting[name];
139+
defining[name] = true;
102140
main.apply(undef, args);
103141
}
142+
143+
if (!defined.hasOwnProperty(name)) {
144+
throw new Error('No ' + name);
145+
}
104146
return defined[name];
105147
}
106148

@@ -136,27 +178,27 @@ var requirejs, require, define;
136178
};
137179
}
138180

181+
function makeConfig(name) {
182+
return function () {
183+
return (config && config.config && config.config[name]) || {};
184+
};
185+
}
186+
139187
main = function (name, deps, callback, relName) {
140188
var args = [],
141189
usingExports,
142-
cjsModule, depName, i, ret, map;
190+
cjsModule, depName, ret, map, i;
143191

144192
//Use name if no relName
145-
if (!relName) {
146-
relName = name;
147-
}
193+
relName = relName || name;
148194

149195
//Call the callback to define the module, if necessary.
150196
if (typeof callback === 'function') {
151197

152-
//Default to require, exports, module if no deps if
153-
//the factory arg has any arguments specified.
154-
if (!deps.length && callback.length) {
155-
deps = ['require', 'exports', 'module'];
156-
}
157-
158198
//Pull out the defined dependencies and pass the ordered
159199
//values to the callback.
200+
//Default to [require, exports, module] if no deps
201+
deps = !deps.length && callback.length ? ['require', 'exports', 'module'] : deps;
160202
for (i = 0; i < deps.length; i++) {
161203
map = makeMap(deps[i], relName);
162204
depName = map.f;
@@ -173,15 +215,16 @@ var requirejs, require, define;
173215
cjsModule = args[i] = {
174216
id: name,
175217
uri: '',
176-
exports: defined[name]
218+
exports: defined[name],
219+
config: makeConfig(name)
177220
};
178221
} else if (defined.hasOwnProperty(depName) || waiting.hasOwnProperty(depName)) {
179222
args[i] = callDep(depName);
180223
} else if (map.p) {
181224
map.p.load(map.n, makeRequire(relName, true), makeLoad(depName), {});
182225
args[i] = defined[depName];
183-
} else {
184-
throw name + ' missing ' + depName;
226+
} else if (!defining[depName]) {
227+
throw new Error(name + ' missing ' + depName);
185228
}
186229
}
187230

@@ -191,9 +234,10 @@ var requirejs, require, define;
191234
//If setting exports via "module" is in play,
192235
//favor that over return value and exports. After that,
193236
//favor a non-undefined return value over exports use.
194-
if (cjsModule && cjsModule.exports !== undef) {
237+
if (cjsModule && cjsModule.exports !== undef &&
238+
cjsModule.exports !== defined[name]) {
195239
defined[name] = cjsModule.exports;
196-
} else if (!usingExports) {
240+
} else if (ret !== undef || !usingExports) {
197241
//Use the return value from the function.
198242
defined[name] = ret;
199243
}
@@ -205,27 +249,30 @@ var requirejs, require, define;
205249
}
206250
};
207251

208-
requirejs = req = function (deps, callback, relName, forceSync) {
252+
requirejs = require = req = function (deps, callback, relName, forceSync) {
209253
if (typeof deps === "string") {
210-
211254
//Just return the module wanted. In this scenario, the
212255
//deps arg is the module name, and second arg (if passed)
213256
//is just the relName.
214257
//Normalize module name, if it contains . or ..
215258
return callDep(makeMap(deps, callback).f);
216259
} else if (!deps.splice) {
217260
//deps is a config object, not an array.
218-
//Drop the config stuff on the ground.
261+
config = deps;
219262
if (callback.splice) {
220263
//callback is an array, which means it is a dependency list.
221264
//Adjust args if there are dependencies
222265
deps = callback;
223-
callback = arguments[2];
266+
callback = relName;
267+
relName = null;
224268
} else {
225-
deps = [];
269+
deps = undef;
226270
}
227271
}
228272

273+
//Support require(['a'])
274+
callback = callback || function () {};
275+
229276
//Simulate async callback;
230277
if (forceSync) {
231278
main(undef, deps, callback, relName);
@@ -242,17 +289,11 @@ var requirejs, require, define;
242289
* Just drops the config on the floor, but returns req in case
243290
* the config return value is used.
244291
*/
245-
req.config = function () {
292+
req.config = function (cfg) {
293+
config = cfg;
246294
return req;
247295
};
248296

249-
/**
250-
* Export require as a global, but only if it does not already exist.
251-
*/
252-
if (!require) {
253-
require = req;
254-
}
255-
256297
define = function (name, deps, callback) {
257298

258299
//This module may not have dependencies
@@ -264,11 +305,7 @@ var requirejs, require, define;
264305
deps = [];
265306
}
266307

267-
if (define.unordered) {
268-
waiting[name] = [name, deps, callback];
269-
} else {
270-
main(name, deps, callback);
271-
}
308+
waiting[name] = [name, deps, callback];
272309
};
273310

274311
define.amd = {

0 commit comments

Comments
 (0)