-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathutils.js
121 lines (104 loc) · 3.25 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
var _ = require('lodash');
var moment = require('moment');
/**
* Extends source dictionaries into the target dictionary
* @param {Object} target Target to extend into
* @param {Objects} sources Sources to extend from
* @return {Object} Returns the target
*/
module.exports.extend = function(target) {
var sources = [].slice.call(arguments, 1);
sources.forEach(function (source) {
for (var prop in source) {
target[prop] = source[prop];
}
});
return target;
};
/**
* Slices a dictionary
* @param {Object} dict Object to slice
* @param {Integer} limit Number of items to return
* @param {Integer} offset Offset to slice to (From limit)
* @return {Object} Sliced dictionary
*/
module.exports.sliceDictionary = function(dict, limit, offset) {
var keys = [];
limit = limit || -1;
offset = offset || -1;
for(var key in dict)
{
if(dict.hasOwnProperty(key))
{
keys.push(key);
}
}
if(limit !== -1 && offset !== -1)
{
keys = keys.slice(offset, offset + limit);
} else if (limit !== -1) {
keys = keys.slice(0, limit);
} else if (offset !== -1) {
keys = keys.slice(offset);
}
var slicedDict = {};
keys.forEach(function(key) {
slicedDict[key] = dict[key];
});
return slicedDict;
};
module.exports.slice = function(data, limit, offset) {
if(Array.isArray(data))
{
return data.slice(offset, limit + offset);
} else {
return this.sliceDictionary(data, limit, offset);
}
};
module.exports.each = function(obj, cb) {
_.each(obj, cb);
};
// Handle custom type path here
// Basically, if there is a custom path defined, throw out newPath and construct it from base
// newPath should be ./.build/<typename> so repace newPath.split('/')[2] with new thing
// Support dates in the url and the typename
// All refer to the publish date of the item
// #Y - Year Full
// #y - Year last two digits
// #m - Month number, leading zero
// #n - Month number, no leading zero
// #F - Month name full (january, october, etc)
// #M - Month short name (jan, oct, etc)
// #d - Day leading zero
// #j - Day, no leading zero
// #T - The typename (e.g. articles)
module.exports.parseCustomUrl = function(url, object) {
var publishDate = object.publish_date ? object.publish_date : object;
publishDate = moment(publishDate);
function replacer(match, timeIdent, offset, string){
if(timeIdent === 'Y') {
return publishDate.format('YYYY').toLowerCase();
} else if (timeIdent === 'y') {
return publishDate.format('YY').toLowerCase();
} else if (timeIdent === 'm') {
return publishDate.format('MM').toLowerCase();
} else if (timeIdent === 'n') {
return publishDate.format('M').toLowerCase();
} else if (timeIdent === 'F') {
return publishDate.format('MMMM').toLowerCase();
} else if (timeIdent === 'M') {
return publishDate.format('MMM').toLowerCase();
} else if (timeIdent === 'd') {
return publishDate.format('DD').toLowerCase();
} else if (timeIdent === 'j') {
return publishDate.format('D').toLowerCase();
} else if (timeIdent === 'T') {
return object._type.toLowerCase();
} else {
return match;
}
}
url = url.replace(/#(\w)/g, replacer);
return url;
}