• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

telefonicaid / iotagent-node-lib / 14218133803

02 Apr 2025 11:08AM UTC coverage: 79.975%. Remained the same
14218133803

Pull #1709

github

web-flow
Merge 31c2853da into ab0df3357
Pull Request #1709: self-content jexl functions:

1970 of 2632 branches covered (74.85%)

Branch coverage included in aggregate %.

68 of 81 new or added lines in 1 file covered. (83.95%)

1 existing line in 1 file now uncovered.

3821 of 4609 relevant lines covered (82.9%)

279.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.23
/lib/jexlTranformsMap.js
1
/*
2
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
3
 *
4
 * This file is part of fiware-iotagent-lib
5
 *
6
 * fiware-iotagent-lib is free software: you can redistribute it and/or
7
 * modify it under the terms of the GNU Affero General Public License as
8
 * published by the Free Software Foundation, either version 3 of the License,
9
 * or (at your option) any later version.
10
 *
11
 * fiware-iotagent-lib is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 * See the GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public
17
 * License along with fiware-iotagent-lib.
18
 * If not, see http://www.gnu.org/licenses/.
19
 *
20
 * For those usages not covered by the GNU Affero General Public License
21
 * please contact with::daniel.moranjimenez@telefonica.com
22
 *
23
 */
24

25
/*This file is intended to contain the whole transformartion map defining  
26
JEXL avaliable transformations*/
27

28
const map = {
1✔
29
    jsonparse: (val) => {
30
        const safeOperation =
31
            (fn) =>
2✔
32
            (...args) => {
2✔
33
                try {
2✔
34
                    return fn(...args);
2✔
35
                } catch (e) {
36
                    return null;
1✔
37
                }
38
            };
39
        return safeOperation(JSON.parse)(val);
2✔
40
    },
41
    jsonstringify: (val) => {
42
        const safeOperation =
43
            (fn) =>
1✔
44
            (...args) => {
1✔
45
                try {
1✔
46
                    return fn(...args);
1✔
47
                } catch (e) {
NEW
48
                    return null;
×
49
                }
50
            };
51
        return safeOperation(JSON.stringify)(val);
1✔
52
    },
53
    indexOf: (val, char) => String(val).indexOf(char),
3✔
54
    length: (val) => String(val).length,
1✔
55
    trim: (val) => String(val).trim(),
11✔
56
    substr: (val, int1, int2) => String(val).substring(int1, int2),
6✔
57
    addreduce: (arr) => {
58
        const safeOperation =
59
            (fn) =>
1✔
60
            (...args) => {
1✔
61
                try {
1✔
62
                    return fn(...args);
1✔
63
                } catch (e) {
64
                    return null;
1✔
65
                }
66
            };
67
        return safeOperation((arr) => arr.reduce((i, v) => i + v))(arr);
1✔
68
    },
69
    lengtharray: (arr) => arr.length,
×
70
    typeof: (val) => typeof val,
×
71
    isarray: Array.isArray,
72
    isnan: isNaN,
73
    parseint: (val) => {
74
        const safeParseNumber = (fn) => (val) => {
1✔
75
            const result = fn(val);
1✔
76
            return isNaN(result) ? null : result;
1!
77
        };
78
        return safeParseNumber((val) => parseInt(val, 10))(val);
1✔
79
    },
80
    parsefloat: (val) => {
81
        const safeParseNumber = (fn) => (val) => {
1✔
82
            const result = fn(val);
1✔
83
            return isNaN(result) ? null : result;
1!
84
        };
85
        return safeParseNumber(parseFloat)(val);
1✔
86
    },
87
    toisodate: (val) => {
88
        const safeDateOperation = (fn) => (val) => {
3✔
89
            const date = new Date(val);
3✔
90
            return isNaN(date.getTime()) ? null : fn(date);
3✔
91
        };
92
        return safeDateOperation((date) => date.toISOString())(val);
3✔
93
    },
94
    timeoffset: (val) => {
95
        const safeDateOperation = (fn) => (val) => {
1✔
96
            const date = new Date(val);
1✔
97
            return isNaN(date.getTime()) ? null : fn(date);
1!
98
        };
99
        return safeDateOperation((date) => date.getTimezoneOffset())(val);
1✔
100
    },
101
    tostring: (val) => {
102
        const safeOperation =
103
            (fn) =>
2✔
104
            (...args) => {
2✔
105
                try {
2✔
106
                    return fn(...args);
2✔
107
                } catch (e) {
108
                    return null;
2✔
109
                }
110
            };
111
        return safeOperation((val) => val.toString())(val);
2✔
112
    },
113
    urlencode: encodeURI,
114
    urldecode: (val) => {
115
        const safeOperation =
116
            (fn) =>
1✔
117
            (...args) => {
1✔
118
                try {
1✔
119
                    return fn(...args);
1✔
120
                } catch (e) {
121
                    return null;
1✔
122
                }
123
            };
124
        return safeOperation(decodeURI)(val);
1✔
125
    },
126
    replacestr: (str, from, to) => str.replace(from, to),
1✔
127
    replaceregexp: (str, reg, to) => {
128
        const safeOperation =
129
            (fn) =>
1✔
130
            (...args) => {
1✔
131
                try {
1✔
132
                    return fn(...args);
1✔
133
                } catch (e) {
134
                    return null;
1✔
135
                }
136
            };
137
        return safeOperation((str, reg, to) => str.replace(new RegExp(reg), to))(str, reg, to);
1✔
138
    },
139
    replaceallregexp: (str, reg, to) => {
140
        const safeOperation =
141
            (fn) =>
1✔
142
            (...args) => {
1✔
143
                try {
1✔
144
                    return fn(...args);
1✔
145
                } catch (e) {
146
                    return null;
1✔
147
                }
148
            };
149
        return safeOperation((str, reg, to) => str.replace(new RegExp(reg, 'g'), to))(str, reg, to);
1✔
150
    },
UNCOV
151
    split: (str, ch) => str.split(ch),
×
152
    joinarrtostr: (arr, ch) => arr.join(ch),
1✔
153
    concatarr: (arr, arr2) => arr.concat(arr2),
2✔
154
    mapper: (val, values, choices) => choices[values.findIndex((target) => target === val)],
×
155
    thmapper: (val, values, choices) =>
156
        choices[values.reduce((acc, curr, i) => (acc !== null ? acc : val <= curr ? i : null), null)],
×
157
    bitwisemask: (i, mask, op, shf) =>
158
        (op === '&' ? parseInt(i) & mask : op === '|' ? parseInt(i) | mask : op === '^' ? parseInt(i) ^ mask : i) >>
×
159
        shf,
160
    slice: (arr, init, end) => arr.slice(init, end),
1✔
161
    addset: (arr, x) => Array.from(new Set(arr).add(x)),
×
162
    removeset: (arr, x) => {
163
        const s = new Set(arr);
×
164
        s.delete(x);
×
165
        return Array.from(s);
×
166
    },
167
    touppercase: (val) => String(val).toUpperCase(),
×
168
    tolowercase: (val) => String(val).toLowerCase(),
×
169
    floor: Math.floor,
170
    ceil: Math.ceil,
171
    round: Math.round,
172
    tofixed: (val, decimals) => {
173
        const num = Number.parseFloat(val);
2✔
174
        const dec = Number.parseInt(decimals);
2✔
175
        return isNaN(num) || isNaN(dec) ? null : num.toFixed(dec);
2!
176
    },
177
    gettime: (val) => {
178
        const safeDateOperation = (fn) => (val) => {
2✔
179
            const date = new Date(val);
2✔
180
            return isNaN(date.getTime()) ? null : fn(date);
2!
181
        };
182
        return safeDateOperation((date) => date.getTime())(val);
2✔
183
    },
184
    toisostring: (val) => {
NEW
185
        const safeDateOperation = (fn) => (val) => {
×
NEW
186
            const date = new Date(val);
×
NEW
187
            return isNaN(date.getTime()) ? null : fn(date);
×
188
        };
NEW
189
        return safeDateOperation((date) => date.toISOString())(val);
×
190
    },
191
    localestring: (d, timezone, options) => {
192
        const safeOperation =
NEW
193
            (fn) =>
×
NEW
194
            (...args) => {
×
NEW
195
                try {
×
NEW
196
                    return fn(...args);
×
197
                } catch (e) {
NEW
198
                    return null;
×
199
                }
200
            };
NEW
201
        return safeOperation((d, timezone, options) => new Date(d).toLocaleString(timezone, options))(
×
202
            d,
203
            timezone,
204
            options
205
        );
206
    },
207
    now: Date.now,
208
    hextostring: (val) => {
209
        const safeOperation =
210
            (fn) =>
1✔
211
            (...args) => {
1✔
212
                try {
1✔
213
                    return fn(...args);
1✔
214
                } catch (e) {
NEW
215
                    return null;
×
216
                }
217
            };
218
        return safeOperation((val) => {
1✔
219
            if (typeof val !== 'string' || !/^[0-9a-fA-F]+$/.test(val) || val.length % 2 !== 0) {
1!
220
                return null;
1✔
221
            }
NEW
222
            return new TextDecoder().decode(new Uint8Array(val.match(/.{1,2}/g).map((byte) => parseInt(byte, 16))));
×
223
        })(val);
224
    },
225
    valuePicker: (val, pick) =>
226
        Object.entries(val)
1✔
227
            .filter(([, v]) => v === pick)
3✔
228
            .map(([k]) => k),
2✔
229
    valuePickerMulti: (val, pick) =>
230
        Object.entries(val)
1✔
231
            .filter(([, v]) => pick.includes(v))
8✔
232
            .map(([k]) => k)
5✔
233
};
234

235
exports.map = map;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc