-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (77 loc) · 2.2 KB
/
Copy pathindex.js
File metadata and controls
81 lines (77 loc) · 2.2 KB
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
const faker = require('faker');
const YAML = require('js-yaml');
const range = require('array-range');
let template = require('backtick-template');
if(typeof template !== 'function') {
template = template.default;
}
class Template {
constructor(template) {
this._template = template;
}
render(vars) {
return template(this._template, vars);
}
}
//
const locationFactory = require('./location/factory.js');
const vars = require('./vars/index.js');
const renderer = require('./lib/render');
const finder = require('./lib/find');
const resolver = require('./lib/resolve');
module.exports = function(seed, path, reader, $, resolve) {
reader = typeof reader === 'undefined' ? read : reader;
$ = typeof $ === 'undefined' ? window.localStorage : $;
resolve = typeof resolve === 'undefined' ? function(path) {
return path[path.length - 1] === '/' ? path.substr(0, path.length - 1) : path;
} : resolve;
return function() {
const mutations = [];
const mutate = function(request, content, config) {
try {
const isJSON = JSON.parse(content);
} catch(e) {
return content;
}
return JSON.stringify(
mutations
.filter(function(item, i, arr) {
// TODO: Deprecate checking on strings, should always be a callable
let cb = item.url;
if (typeof item.url !== 'function') {
cb = function(actual) {
return item.url === request.url;
};
}
// TODO: Deprecate passing 2 arguments pass a request object instead
return cb(request.url, request.method);
})
.reduce(function(prev, item, i, arr) {
return item.mutate(prev, config);
}, JSON.parse(content))
);
};
const addMutation = function(cb, url) {
mutations.push({
url: url,
mutate: cb,
});
};
const controller = require('./lib/controller')(
resolver(resolve, path),
finder(reader, path),
renderer(
vars(locationFactory(), range, $),
Template,
faker,
seed
),
YAML,
mutate
);
return {
serve: controller,
mutate: addMutation,
};
};
};