MCPcopy Create free account
hub / github.com/PrairieLearn/PrairieLearn / normalize

Function normalize

public/javascripts/require.js:267–354  ·  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

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

Callers 2

makeModuleMapFunction · 0.85
newContextFunction · 0.85

Calls 2

trimDotsFunction · 0.85
getOwnFunction · 0.85

Tested by

no test coverage detected