UNPKG

1.12 kBJavaScriptView Raw
1/*eslint no-unused-vars: "off" */
2// The libraries for 'moment' and 'lodash' are available inside the mutator definition
3const moment = require('moment');
4
5const OLD_DATE_FORMAT = 'YYYY-MM-DD';
6const OLD_DATE_REGEX = /[0-9]{4}-[0-9]{2}-[0-9]{2}/;
7const NEW_DATE_FORMAT = 'YYYY-MM';
8
9module.exports = {
10 /**
11 * Type of mutator
12 */
13 type: 'data',
14 /**
15 * The predicate function is called for every target document
16 * @param doc - The document to be checked against the predicate
17 * @param arguments - The task-specific arguments object
18 * @returns {boolean}
19 */
20 predicate: function (doc, arguments) {
21 return OLD_DATE_REGEX.test(doc._index);
22 },
23 /**
24 * The mutate function is only called on documents that satisfy the predicate
25 * @param doc - The document that satisfied the predicate
26 * @param arguments - The task-specific arguments object
27 * @returns {*}
28 */
29 mutate: function (doc, arguments) {
30 const date = moment(doc._index.match(OLD_DATE_REGEX), OLD_DATE_FORMAT);
31 doc._index = doc._index.replace(OLD_DATE_REGEX, date.format(NEW_DATE_FORMAT));
32
33 return doc;
34 }
35};
\No newline at end of file