MCPcopy Create free account
hub / github.com/edc/mapcat / normalize

Function normalize

example/almond.js:33–138  ·  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. * @returns {String} normalized name

(name, baseName)

Source from the content-addressed store, hash-verified

31 * @returns {String} normalized name
32 */
33 function normalize(name, baseName) {
34 var nameParts, nameSegment, mapValue, foundMap,
35 foundI, foundStarMap, starI, i, j, part,
36 baseParts = baseName && baseName.split("/"),
37 map = config.map,
38 starMap = (map && map['*']) || {};
39
40 //Adjust any relative paths.
41 if (name && name.charAt(0) === ".") {
42 //If have a base name, try to normalize against it,
43 //otherwise, assume it is a top-level require that will
44 //be relative to baseUrl in the end.
45 if (baseName) {
46 //Convert baseName to array, and lop off the last part,
47 //so that . matches that "directory" and not name of the baseName's
48 //module. For instance, baseName of "one/two/three", maps to
49 //"one/two/three.js", but we want the directory, "one/two" for
50 //this normalization.
51 baseParts = baseParts.slice(0, baseParts.length - 1);
52
53 name = baseParts.concat(name.split("/"));
54
55 //start trimDots
56 for (i = 0; i < name.length; i += 1) {
57 part = name[i];
58 if (part === ".") {
59 name.splice(i, 1);
60 i -= 1;
61 } else if (part === "..") {
62 if (i === 1 && (name[2] === '..' || name[0] === '..')) {
63 //End of the line. Keep at least one non-dot
64 //path segment at the front so it can be mapped
65 //correctly to disk. Otherwise, there is likely
66 //no path mapping for a path starting with '..'.
67 //This can still fail, but catches the most reasonable
68 //uses of ..
69 break;
70 } else if (i > 0) {
71 name.splice(i - 1, 2);
72 i -= 2;
73 }
74 }
75 }
76 //end trimDots
77
78 name = name.join("/");
79 } else if (name.indexOf('./') === 0) {
80 // No baseName, so this is ID is resolved relative
81 // to baseUrl, pull off the leading dot.
82 name = name.substring(2);
83 }
84 }
85
86 //Apply map config if available.
87 if ((baseParts || starMap) && map) {
88 nameParts = name.split('/');
89
90 for (i = nameParts.length; i > 0; i -= 1) {

Callers 2

makeNormalizeFunction · 0.85
almond.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected