1 | (function() {
|
2 | var FluentDate, _, lo;
|
3 |
|
4 | FluentDate = require('../core/fluent-date');
|
5 |
|
6 | _ = require('underscore');
|
7 |
|
8 | lo = require('lodash');
|
9 |
|
10 | module.exports = {
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | run: function(rez, collKey, startKey, endKey, unit) {
|
19 | var firstDate, hist, lastDate, new_e;
|
20 | unit = unit || 'years';
|
21 | hist = lo.get(rez, collKey);
|
22 | if (!hist || !hist.length) {
|
23 | return 0;
|
24 | }
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | new_e = hist.map(function(job) {
|
31 | var obj;
|
32 | obj = _.pick(job, [startKey, endKey]);
|
33 | if (!_.has(obj, endKey)) {
|
34 |
|
35 | obj[endKey] = 'current';
|
36 | }
|
37 | if (obj && (obj[startKey] || obj[endKey])) {
|
38 | obj = _.pairs(obj);
|
39 | obj[0][1] = FluentDate.fmt(obj[0][1]);
|
40 | if (obj.length > 1) {
|
41 | obj[1][1] = FluentDate.fmt(obj[1][1]);
|
42 | }
|
43 | }
|
44 | return obj;
|
45 | });
|
46 |
|
47 | new_e = _.filter(_.flatten(new_e, true), function(v) {
|
48 | return v && v.length && v[0] && v[0].length;
|
49 | });
|
50 | if (!new_e || !new_e.length) {
|
51 | return 0;
|
52 | }
|
53 | new_e = _.sortBy(new_e, function(elem) {
|
54 | return elem[1].unix();
|
55 | });
|
56 |
|
57 | firstDate = _.first(new_e)[1];
|
58 | lastDate = _.last(new_e)[1];
|
59 | return lastDate.diff(firstDate, unit);
|
60 | }
|
61 | };
|
62 |
|
63 | }).call(this);
|
64 |
|
65 |
|