UNPKG

3.12 kBJavaScriptView Raw
1const generateUUID = function() {
2 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
3 var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
4 return v.toString(16);
5 });
6};
7
8const isPermalink = function(string) {
9 return string.match(/^(?:\/[^\/\?]+)+\/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/);
10};
11
12const splitPermalink = function(permalink) {
13 const parts = permalink.split('/');
14 return {
15 path: parts.slice(0, parts.length-1).join('/'),
16 key: parts[parts.length-1]
17 };
18};
19
20const getKeyFromPermalink = function(permalink) {
21 return splitPermalink(permalink).key;
22};
23
24const getPathFromPermalink = function(permalink) {
25 return splitPermalink(permalink).path;
26};
27
28const replaceSpecialCharacters = function (string) {
29 return string
30 .replace(/[\u00C0-\u00C5\u00E0-\u00E5]/g, 'a')
31 .replace(/[\u00C6\u00E6]/g, 'ae')
32 .replace(/[\u00C7\u00E7]/g, 'c')
33 .replace(/[\u00C8-\u00CB\u00E8-\u00EB]/g, 'e')
34 .replace(/[\u00CC-\u00CF\u00EC-\u00EF]/g, 'i')
35 .replace(/[\u00D1\u00F1]/g, 'n')
36 .replace(/[\u00D2-\u00D6\u00D8\u00F2-\u00F6\u00F8]/g, 'o')
37 .replace(/[\u00D9-\u00DC\u00F9-\u00FC]/g, 'u')
38 .replace(/[\u00DD\u00FD\u00FF]/g, 'y')
39 .replace(/[\u00DF]/g, 'ss')
40 .replace(/[ł]/g, 'l')
41 .replace(/[^a-zA-Z0-9\-]/g, '');
42 };
43
44const strip$$Properties = function (obj) {
45 if(!obj || typeof obj !== 'object') {
46 return obj;
47 }
48 if(Array.isArray(obj)) {
49 const newArray = [];
50 obj.forEach(elem => {
51 newArray.push(strip$$Properties(elem));
52 });
53 return newArray;
54 } else {
55 const newObj = {};
56 Object.keys(obj).forEach(function(key) {
57 if(!key.match(/^\$\$/)) {
58 newObj[key] = strip$$Properties(obj[key]);
59 }
60 });
61 return newObj;
62 }
63};
64
65const strip$$PropertiesFromObject = function(obj, newBatch) {
66 newBatch.push({
67 href: obj.href,
68 verb: obj.verb,
69 body: strip$$Properties(obj.body)
70 });
71};
72
73const strip$$PropertiesFromBatch = function (batch) {
74 if(!batch) {
75 return batch;
76 }
77 const newBatch = [];
78 for(let obj of batch) {
79 if(Array.isArray(obj)) {
80 const newSubBatch = [];
81 for(let subObj of obj) {
82 strip$$PropertiesFromObject(subObj, newSubBatch);
83 }
84 newBatch.push(newSubBatch);
85 } else {
86 strip$$PropertiesFromObject(obj, newBatch);
87 }
88 }
89 return newBatch;
90};
91
92const paramsToString = function (path, params) {
93 var ret = path;
94 for (var key in params) {
95 if (params.hasOwnProperty(key) && params[key]) {
96 if(!ret.match(/\?/g)) {
97 ret += '?';
98 } else {
99 ret += '&';
100 }
101 ret += encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
102 }
103 }
104 return ret;
105};
106
107module.exports = {
108 generateUUID: generateUUID,
109 isPermalink: isPermalink,
110 splitPermalink: splitPermalink,
111 getKeyFromPermalink: getKeyFromPermalink,
112 getPathFromPermalink: getPathFromPermalink,
113 parametersToString: paramsToString,
114 replaceSpecialCharacters: replaceSpecialCharacters,
115 strip$$Properties: strip$$Properties,
116 strip$$PropertiesFromBatch: strip$$PropertiesFromBatch
117};
\No newline at end of file