summaryrefslogtreecommitdiffstats
path: root/lib/utils
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/__tests__/location.js15
-rw-r--r--lib/utils/location.js86
-rw-r--r--lib/utils/promise.js87
-rw-r--r--lib/utils/reducedObject.js5
4 files changed, 115 insertions, 78 deletions
diff --git a/lib/utils/__tests__/location.js b/lib/utils/__tests__/location.js
index 2d01714..822338e 100644
--- a/lib/utils/__tests__/location.js
+++ b/lib/utils/__tests__/location.js
@@ -39,6 +39,21 @@ describe('LocationUtils', function() {
});
});
+ describe('.flatten', function() {
+ it('should remove leading slash', function() {
+ expect(LocationUtils.flatten('/test.md')).toBe('test.md');
+ expect(LocationUtils.flatten('/hello/cool.md')).toBe('hello/cool.md');
+ });
+
+ it('should remove leading slashes', function() {
+ expect(LocationUtils.flatten('///test.md')).toBe('test.md');
+ });
+
+ it('should not break paths', function() {
+ expect(LocationUtils.flatten('hello/cool.md')).toBe('hello/cool.md');
+ });
+ });
+
describe('.toAbsolute', function() {
it('should correctly transform as absolute', function() {
expect(LocationUtils.toAbsolute('http://google.fr')).toBe('http://google.fr');
diff --git a/lib/utils/location.js b/lib/utils/location.js
index 17edc00..00d8004 100644
--- a/lib/utils/location.js
+++ b/lib/utils/location.js
@@ -40,13 +40,28 @@ function normalize(s) {
}
/**
- Convert a relative path to absolute
+ * Flatten a path, it removes the leading "/"
+ *
+ * @param {String} href
+ * @return {String}
+ */
+function flatten(href) {
+ href = normalize(href);
+ if (href[0] == '/') {
+ href = normalize(href.slice(1));
+ }
+
+ return href;
+}
- @param {String} href
- @param {String} dir: directory parent of the file currently in rendering process
- @param {String} outdir: directory parent from the html output
- @return {String}
-*/
+/**
+ * Convert a relative path to absolute
+ *
+ * @param {String} href
+ * @param {String} dir: directory parent of the file currently in rendering process
+ * @param {String} outdir: directory parent from the html output
+ * @return {String}
+ */
function toAbsolute(_href, dir, outdir) {
if (isExternal(_href) || isDataURI(_href)) {
return _href;
@@ -74,50 +89,51 @@ function toAbsolute(_href, dir, outdir) {
}
/**
- Convert an absolute path to a relative path for a specific folder (dir)
- ('test/', 'hello.md') -> '../hello.md'
-
- @param {String} dir: current directory
- @param {String} file: absolute path of file
- @return {String}
-*/
+ * Convert an absolute path to a relative path for a specific folder (dir)
+ * ('test/', 'hello.md') -> '../hello.md'
+ *
+ * @param {String} dir: current directory
+ * @param {String} file: absolute path of file
+ * @return {String}
+ */
function relative(dir, file) {
var isDirectory = file.slice(-1) === '/';
return normalize(path.relative(dir, file)) + (isDirectory? '/': '');
}
/**
- Convert an absolute path to a relative path for a specific folder (dir)
- ('test/test.md', 'hello.md') -> '../hello.md'
-
- @param {String} baseFile: current file
- @param {String} file: absolute path of file
- @return {String}
-*/
+ * Convert an absolute path to a relative path for a specific folder (dir)
+ * ('test/test.md', 'hello.md') -> '../hello.md'
+ *
+ * @param {String} baseFile: current file
+ * @param {String} file: absolute path of file
+ * @return {String}
+ */
function relativeForFile(baseFile, file) {
return relative(path.dirname(baseFile), file);
}
/**
- Compare two paths, return true if they are identical
- ('README.md', './README.md') -> true
-
- @param {String} p1: first path
- @param {String} p2: second path
- @return {Boolean}
-*/
+ * Compare two paths, return true if they are identical
+ * ('README.md', './README.md') -> true
+ *
+ * @param {String} p1: first path
+ * @param {String} p2: second path
+ * @return {Boolean}
+ */
function areIdenticalPaths(p1, p2) {
return normalize(p1) === normalize(p2);
}
module.exports = {
areIdenticalPaths: areIdenticalPaths,
- isDataURI: isDataURI,
- isExternal: isExternal,
- isRelative: isRelative,
- isAnchor: isAnchor,
- normalize: normalize,
- toAbsolute: toAbsolute,
- relative: relative,
- relativeForFile: relativeForFile
+ isDataURI: isDataURI,
+ isExternal: isExternal,
+ isRelative: isRelative,
+ isAnchor: isAnchor,
+ normalize: normalize,
+ toAbsolute: toAbsolute,
+ relative: relative,
+ relativeForFile: relativeForFile,
+ flatten: flatten
};
diff --git a/lib/utils/promise.js b/lib/utils/promise.js
index 138546b..b5cca4b 100644
--- a/lib/utils/promise.js
+++ b/lib/utils/promise.js
@@ -2,34 +2,35 @@ var Q = require('q');
var Immutable = require('immutable');
// Debugging for long stack traces
-if (global.__DEV__ || process.env.DEBUG) {
+if (process.env.DEBUG || process.env.CI) {
Q.longStackSupport = true;
}
/**
- Reduce an array to a promise
-
- @param {Array|List} arr
- @param {Function(value, element, index)}
- @return {Promise<Mixed>}
-*/
+ * Reduce an array to a promise
+ *
+ * @param {Array|List} arr
+ * @param {Function(value, element, index)}
+ * @return {Promise<Mixed>}
+ */
function reduce(arr, iter, base) {
arr = Immutable.Iterable.isIterable(arr)? arr : Immutable.List(arr);
return arr.reduce(function(prev, elem, key) {
- return prev.then(function(val) {
+ return prev
+ .then(function(val) {
return iter(val, elem, key);
});
}, Q(base));
}
/**
- Iterate over an array using an async iter
-
- @param {Array|List} arr
- @param {Function(value, element, index)}
- @return {Promise}
-*/
+ * Iterate over an array using an async iter
+ *
+ * @param {Array|List} arr
+ * @param {Function(value, element, index)}
+ * @return {Promise}
+ */
function forEach(arr, iter) {
return reduce(arr, function(val, el, key) {
return iter(el, key);
@@ -37,12 +38,12 @@ function forEach(arr, iter) {
}
/**
- Transform an array
-
- @param {Array|List} arr
- @param {Function(value, element, index)}
- @return {Promise}
-*/
+ * Transform an array
+ *
+ * @param {Array|List} arr
+ * @param {Function(value, element, index)}
+ * @return {Promise}
+ */
function serie(arr, iter, base) {
return reduce(arr, function(before, item, key) {
return Q(iter(item, key))
@@ -54,12 +55,12 @@ function serie(arr, iter, base) {
}
/**
- Iter over an array and return first result (not null)
-
- @param {Array|List} arr
- @param {Function(element, index)}
- @return {Promise<Mixed>}
-*/
+ * Iter over an array and return first result (not null)
+ *
+ * @param {Array|List} arr
+ * @param {Function(element, index)}
+ * @return {Promise<Mixed>}
+ */
function some(arr, iter) {
arr = Immutable.List(arr);
@@ -73,12 +74,12 @@ function some(arr, iter) {
}
/**
- Map an array using an async (promised) iterator
-
- @param {Array|List} arr
- @param {Function(element, index)}
- @return {Promise<List>}
-*/
+ * Map an array using an async (promised) iterator
+ *
+ * @param {Array|List} arr
+ * @param {Function(element, index)}
+ * @return {Promise<List>}
+ */
function mapAsList(arr, iter) {
return reduce(arr, function(prev, entry, i) {
return Q(iter(entry, i))
@@ -90,12 +91,12 @@ function mapAsList(arr, iter) {
}
/**
- Map an array or map
-
- @param {Array|List|Map|OrderedMap} arr
- @param {Function(element, key)}
- @return {Promise<List|Map|OrderedMap>}
-*/
+ * Map an array or map
+ *
+ * @param {Array|List|Map|OrderedMap} arr
+ * @param {Function(element, key)}
+ * @return {Promise<List|Map|OrderedMap>}
+ */
function map(arr, iter) {
if (Immutable.Map.isMap(arr)) {
var type = 'Map';
@@ -122,11 +123,11 @@ function map(arr, iter) {
/**
- Wrap a function in a promise
-
- @param {Function} func
- @return {Funciton}
-*/
+ * Wrap a function in a promise
+ *
+ * @param {Function} func
+ * @return {Funciton}
+ */
function wrap(func) {
return function() {
var args = Array.prototype.slice.call(arguments, 0);
diff --git a/lib/utils/reducedObject.js b/lib/utils/reducedObject.js
index fa5d32c..7bcfd5b 100644
--- a/lib/utils/reducedObject.js
+++ b/lib/utils/reducedObject.js
@@ -4,8 +4,13 @@ var Immutable = require('immutable');
* Reduce the difference between a map and its default version
* @param {Map} defaultVersion
* @param {Map} currentVersion
+ * @return {Map} The properties of currentVersion that differs from defaultVersion
*/
function reducedObject(defaultVersion, currentVersion) {
+ if(defaultVersion === undefined) {
+ return currentVersion;
+ }
+
return currentVersion.reduce(function(result, value, key) {
var defaultValue = defaultVersion.get(key);