-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
153 lines (148 loc) · 5.53 KB
/
index.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var detalle = require('senadores-detalle')
var asistencia = require('senadores-asistencia')
var viajes = require('senadores-viajes')
var elecciones = require('senadores-elecciones')
var eleccionesP = function () {
var args = arguments
var self = this
return new Promise((resolve, reject) => {
try {
resolve(elecciones.apply(self, args))
} catch (ex) {
reject(ex)
}
})
}
module.exports = function senadores (opts, type) {
type = type || 'default'
if (type === 'default') {
return detalle(opts)
} else if (type === 'asistencia') {
return asistencia(opts, { incluyeSenador: !!opts.incluyeSenador })
} else if (type === 'asistencia.sala') {
if (opts.asistenciaSala && typeof opts.asistenciaSala === 'string') {
return asistencia(opts, { tipo: 'sala', incluyeSenador: !!opts.incluyeSenador }).then(asistencias => {
var total = asistencias[0].asistencia + asistencias[0].inasistencias.total
var filter = parseCondition(opts.asistenciaSala, 'asistencia', total)
var array = asistencias.filter(filter)
return Promise.resolve(array)
})
}
return asistencia(opts, { tipo: 'sala', incluyeSenador: !!opts.incluyeSenador })
} else if (type === 'asistencia.comision' || type === 'asistencia.comisiones') {
return opts.periodoAsistenciaComisiones
? asistencia(opts, { tipo: 'comision', periodo: opts.periodoAsistenciaComisiones, incluyeSenador: !!opts.incluyeSenador })
: asistencia(opts, { tipo: 'comision', incluyeSenador: !!opts.incluyeSenador })
} else if (type === 'viajes') {
// por ahora se muestran solo viajes al extranjero
return opts.periodoViajesInternacionales
? viajes(opts, { tipo: 'extranjeros', periodo: opts.periodoViajesInternacionales })
: viajes(opts, { tipo: 'extranjeros' })
} else if (type === 'viajes.internacionales') {
return opts.periodoViajesInternacionales
? viajes(opts, { tipo: 'extranjeros', periodo: opts.periodoViajesInternacionales, incluyeSenador: !!opts.incluyeSenador })
: viajes(opts, { tipo: 'extranjeros', incluyeSenador: !!opts.incluyeSenador })
} else if (type === 'elecciones') {
return eleccionesP(opts, { tipo: 'elecciones', incluyeSenador: !!opts.incluyeSenador })
} else if (type === 'elecciones.gastos') {
if (opts.gastosElecciones && typeof opts.gastosElecciones === 'string') {
return eleccionesP(opts, { tipo: 'gastos', incluyeSenador: !!opts.incluyeSenador }).then(gastos => {
var filter = parseCondition(opts.gastosElecciones, opts.incluyeSenador ? 'elecciones.gastos.total' : 'total')
var array = gastos.filter(filter)
return Promise.resolve(array)
})
}
return eleccionesP(opts, { tipo: 'gastos', incluyeSenador: !!opts.incluyeSenador })
} else if (type === 'elecciones.ingresos') {
if (opts.ingresosElecciones && typeof opts.ingresosElecciones === 'string') {
return eleccionesP(opts, { tipo: 'ingresos', incluyeSenador: !!opts.incluyeSenador }).then(ingresos => {
var filter = parseCondition(opts.ingresosElecciones, opts.incluyeSenador ? 'elecciones.ingresos.total' : 'total')
var array = ingresos.filter(filter)
return Promise.resolve(array)
})
}
return eleccionesP(opts, { tipo: 'ingresos', incluyeSenador: !!opts.incluyeSenador })
}
}
function parseCondition (condition, property, total) {
total = total || 1
// get operator
var regex = /([<|>|=]{1,2})((?:\.\d+)|(?:\d+\.\d*)|(?:\d*))(%)?/ // /([<|>|=]{1,2})(\.?)(\d*)(%)?/
var arr = regex.exec(condition)
var operator = arr[1]
var value = parseFloat(arr[2])
var isPercentage = arr[3] === '%'
var isRatio = ((value >= 0) && (value <= 1))
switch (operator) {
case '<':
return (item) => {
var val = getProperty(item, property)
if (val) {
return isPercentage
? val < (value * total) / 100
: isRatio
? val < value * total
: val < value
}
return false
}
case '>':
return (item) => {
var val = getProperty(item, property)
if (val) {
return isPercentage
? val > (value * total) / 100
: isRatio
? val > value * total
: val > value
}
return false
}
case '<=':
return (item) => {
var val = getProperty(item, property)
if (val) {
return isPercentage
? val <= (value * total) / 100
: isRatio
? val <= value * total
: val <= value
}
return false
}
case '>=':
return (item) => {
var val = getProperty(item, property)
if (val) {
return isPercentage
? val >= (value * total) / 100
: isRatio
? val >= value * total
: val >= value
}
return false
}
case '=':
return (item) => {
var val = getProperty(item, property)
if (val) {
return isPercentage
? val === (value * total) / 100
: isRatio
? val === value * total
: val === value
}
return false
}
default:
return () => false
}
function getProperty (item, prop) {
var props = prop.split('.')
var value = item
props.forEach(property => {
if (value) value = value[property]
})
return value
}
}