MCPcopy Create free account
hub / github.com/JsAaron/jQuery / normalize

Function normalize

迷你jQuery/basis/Require.js:268–361  ·  view source on GitHub ↗

* Given a relative module name, like ./something, normalize it to * a real name that can be mapped to a path. * @param {String} name the relative name * @param {String} baseName a real name that the name arg is relative * to. * @param {Boolean} applyMap a

(name, baseName, applyMap)

Source from the content-addressed store, hash-verified

266 * @returns {String} normalized name
267 */
268 function normalize(name, baseName, applyMap) {
269 var pkgMain, mapValue, nameParts, i, j, nameSegment, lastIndex,
270 foundMap, foundI, foundStarMap, starI,
271 baseParts = baseName && baseName.split('/'),
272 normalizedBaseParts = baseParts,
273 map = config.map,
274 starMap = map && map['*'];
275
276 //Adjust any relative paths.
277 if (name && name.charAt(0) === '.') {
278 //If have a base name, try to normalize against it,
279 //otherwise, assume it is a top-level require that will
280 //be relative to baseUrl in the end.
281 if (baseName) {
282 //Convert baseName to array, and lop off the last part,
283 //so that . matches that 'directory' and not name of the baseName's
284 //module. For instance, baseName of 'one/two/three', maps to
285 //'one/two/three.js', but we want the directory, 'one/two' for
286 //this normalization.
287 normalizedBaseParts = baseParts.slice(0, baseParts.length - 1);
288 name = name.split('/');
289 lastIndex = name.length - 1;
290
291 // If wanting node ID compatibility, strip .js from end
292 // of IDs. Have to do this here, and not in nameToUrl
293 // because node allows either .js or non .js to map
294 // to same file.
295 if (config.nodeIdCompat && jsSuffixRegExp.test(name[lastIndex])) {
296 name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, '');
297 }
298
299 name = normalizedBaseParts.concat(name);
300 trimDots(name);
301 name = name.join('/');
302 } else if (name.indexOf('./') === 0) {
303 // No baseName, so this is ID is resolved relative
304 // to baseUrl, pull off the leading dot.
305 name = name.substring(2);
306 }
307 }
308
309 //Apply map config if available.
310 if (applyMap && map && (baseParts || starMap)) {
311 nameParts = name.split('/');
312
313 outerLoop: for (i = nameParts.length; i > 0; i -= 1) {
314 nameSegment = nameParts.slice(0, i).join('/');
315
316 if (baseParts) {
317 //Find the longest baseName segment match in the config.
318 //So, do joins on the biggest to smallest lengths of baseParts.
319 for (j = baseParts.length; j > 0; j -= 1) {
320 mapValue = getOwn(map, baseParts.slice(0, j).join('/'));
321
322 //baseName segment has config, find if it has one for
323 //this name.
324 if (mapValue) {
325 mapValue = getOwn(mapValue, nameSegment);

Callers 2

makeModuleMapFunction · 0.85
newContextFunction · 0.85

Calls 2

trimDotsFunction · 0.85
getOwnFunction · 0.85

Tested by

no test coverage detected