@questwork/q-utilities 0.1.8 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.cjs +194 -158
- package/dist/index.min.js +194 -158
- package/package.json +7 -7
package/dist/index.min.cjs
CHANGED
|
@@ -961,11 +961,11 @@ class Repo {
|
|
|
961
961
|
reject(new Error('more than one is found'))
|
|
962
962
|
}
|
|
963
963
|
})
|
|
964
|
-
.catch((err) => {
|
|
965
|
-
log({ level: 'warn', output: err.toString() })
|
|
966
|
-
throw err
|
|
967
|
-
})
|
|
968
964
|
})
|
|
965
|
+
.catch((err) => {
|
|
966
|
+
log({ level: 'warn', output: err.toString() })
|
|
967
|
+
throw err
|
|
968
|
+
})
|
|
969
969
|
}
|
|
970
970
|
|
|
971
971
|
saveAll({ docs, systemLog }) {
|
|
@@ -979,14 +979,14 @@ class Repo {
|
|
|
979
979
|
const promise = typeof this.model.saveAll === 'function'
|
|
980
980
|
? this.model.saveAll({ docs })
|
|
981
981
|
: Promise.all(docs.map(async (doc) => {
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
982
|
+
if (doc) {
|
|
983
|
+
const result = await this.saveOne({ doc })
|
|
984
|
+
isNew = result.isNew
|
|
985
|
+
const _data = result._data || result.data
|
|
986
|
+
return _data[0]
|
|
987
|
+
}
|
|
988
|
+
return null
|
|
989
|
+
}))
|
|
990
990
|
return promise.then((savedData) => {
|
|
991
991
|
if (savedData.length !== 1) isNew = null
|
|
992
992
|
const result = {
|
|
@@ -1146,6 +1146,27 @@ function makeService({ repo }) {
|
|
|
1146
1146
|
|
|
1147
1147
|
|
|
1148
1148
|
|
|
1149
|
+
;// ./lib/models/template/templateException.js
|
|
1150
|
+
const TEMPLATE_EXCEPTION_TYPE = {
|
|
1151
|
+
argumentEmptyException: 'Argument is empty',
|
|
1152
|
+
argumentFormatException: 'Incorrect number or format of argument',
|
|
1153
|
+
invalidFuntionException: 'Function Name is invalid',
|
|
1154
|
+
invalidRegExpException: 'Invalid regular expression',
|
|
1155
|
+
isNotAFunctionException: 'Is not a function',
|
|
1156
|
+
notExistException: 'Key does not exist',
|
|
1157
|
+
resultEmptyException: 'Result is empty',
|
|
1158
|
+
resultMoreThanOneException: 'More than one result'
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
class TemplateException extends Error {
|
|
1162
|
+
constructor(message) {
|
|
1163
|
+
super(message)
|
|
1164
|
+
this.message = message
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
|
|
1149
1170
|
;// ./lib/models/template/constants.js
|
|
1150
1171
|
const _EMPTY = '_EMPTY'
|
|
1151
1172
|
const _FN_NAMES = [
|
|
@@ -1162,8 +1183,13 @@ const TAGS_HANDLEBAR = ['{{', '}}']
|
|
|
1162
1183
|
;// ./lib/models/template/helpers/_eq.js
|
|
1163
1184
|
|
|
1164
1185
|
|
|
1186
|
+
|
|
1187
|
+
|
|
1165
1188
|
function _eq(data, args) {
|
|
1166
|
-
if (
|
|
1189
|
+
if (args.length !== 3) {
|
|
1190
|
+
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentFormatException)
|
|
1191
|
+
}
|
|
1192
|
+
if (data === null || (typeof data === 'undefined')) {
|
|
1167
1193
|
return null
|
|
1168
1194
|
}
|
|
1169
1195
|
if (args.includes(_SELF)) {
|
|
@@ -1177,46 +1203,29 @@ function _eq(data, args) {
|
|
|
1177
1203
|
|
|
1178
1204
|
|
|
1179
1205
|
|
|
1180
|
-
;// ./lib/models/template/templateException.js
|
|
1181
|
-
const TEMPLATE_EXCEPTION_TYPE = {
|
|
1182
|
-
argumentEmptyException: 'Argument is empty',
|
|
1183
|
-
argumentFormatException: 'Incorrect number or format of argument',
|
|
1184
|
-
invalidFuntionException: 'Function Name is invalid',
|
|
1185
|
-
invalidRegExpException: 'Invalid regular expression',
|
|
1186
|
-
isNotAFunctionException: 'Is not a function',
|
|
1187
|
-
notExistException: 'Key does not exist',
|
|
1188
|
-
resultEmptyException: 'Result is empty',
|
|
1189
|
-
resultMoreThanOneException: 'More than one result'
|
|
1190
|
-
}
|
|
1191
|
-
|
|
1192
|
-
class TemplateException extends Error {
|
|
1193
|
-
constructor(message) {
|
|
1194
|
-
super(message)
|
|
1195
|
-
this.message = message
|
|
1196
|
-
}
|
|
1197
|
-
}
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
1206
|
;// ./lib/models/template/helpers/_filterAll.js
|
|
1202
1207
|
|
|
1203
1208
|
|
|
1204
1209
|
const _filterAll_DELIMITER = '~~~'
|
|
1205
1210
|
|
|
1206
1211
|
function _filterAll(data, args) {
|
|
1207
|
-
|
|
1212
|
+
try {
|
|
1213
|
+
if (!Array.isArray(args) || args.length === 0) {
|
|
1214
|
+
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
|
|
1215
|
+
}
|
|
1216
|
+
if (!Array.isArray(data) || data.length === 0) {
|
|
1217
|
+
return []
|
|
1218
|
+
}
|
|
1219
|
+
if (typeof data[0] === 'object') {
|
|
1220
|
+
return _existObject(data, args)
|
|
1221
|
+
}
|
|
1222
|
+
if (typeof data[0] === 'string' || typeof data[0] === 'number') {
|
|
1223
|
+
return _exist(data, args)
|
|
1224
|
+
}
|
|
1208
1225
|
return []
|
|
1226
|
+
} catch (e) {
|
|
1227
|
+
throw e
|
|
1209
1228
|
}
|
|
1210
|
-
|
|
1211
|
-
if (typeof data[0] === 'object') {
|
|
1212
|
-
return _existObject(data, args)
|
|
1213
|
-
}
|
|
1214
|
-
|
|
1215
|
-
if (typeof data[0] === 'string' || typeof data[0] === 'number') {
|
|
1216
|
-
return _exist(data, args)
|
|
1217
|
-
}
|
|
1218
|
-
|
|
1219
|
-
return []
|
|
1220
1229
|
}
|
|
1221
1230
|
|
|
1222
1231
|
function _exist(data, args) {
|
|
@@ -1243,11 +1252,18 @@ function _existObject(data, args) {
|
|
|
1243
1252
|
|
|
1244
1253
|
|
|
1245
1254
|
function _filterOne(data, args) {
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1255
|
+
try {
|
|
1256
|
+
const list = _filterAll(data, args)
|
|
1257
|
+
if (list.length === 1) {
|
|
1258
|
+
return list[0]
|
|
1259
|
+
}
|
|
1260
|
+
if (list.length === 0) {
|
|
1261
|
+
return null
|
|
1262
|
+
}
|
|
1263
|
+
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.resultMoreThanOneException)
|
|
1264
|
+
} catch (e) {
|
|
1265
|
+
throw e
|
|
1249
1266
|
}
|
|
1250
|
-
return null
|
|
1251
1267
|
}
|
|
1252
1268
|
|
|
1253
1269
|
|
|
@@ -1256,28 +1272,31 @@ function _filterOne(data, args) {
|
|
|
1256
1272
|
|
|
1257
1273
|
|
|
1258
1274
|
function _get(data, key, failover = null) {
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
if (
|
|
1267
|
-
const
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1275
|
+
try {
|
|
1276
|
+
if (key === null || (typeof key === 'undefined') || key === '') {
|
|
1277
|
+
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
|
|
1278
|
+
}
|
|
1279
|
+
if (data === null) {
|
|
1280
|
+
return null
|
|
1281
|
+
}
|
|
1282
|
+
if (key.includes('.')) {
|
|
1283
|
+
const parts = key.split('.')
|
|
1284
|
+
if (parts.length > 1) {
|
|
1285
|
+
const first = parts.shift()
|
|
1286
|
+
const remainingKey = parts.join('.')
|
|
1287
|
+
if (typeof data[first] !== 'undefined') {
|
|
1288
|
+
return _get(data[first], remainingKey, failover)
|
|
1289
|
+
}
|
|
1290
|
+
return _handleFailover(key, failover)
|
|
1271
1291
|
}
|
|
1272
|
-
return _handleFailover(key, failover)
|
|
1273
1292
|
}
|
|
1293
|
+
if (typeof data[key] !== 'undefined') {
|
|
1294
|
+
return data[key]
|
|
1295
|
+
}
|
|
1296
|
+
return _handleFailover(key, failover)
|
|
1297
|
+
} catch (e) {
|
|
1298
|
+
throw e
|
|
1274
1299
|
}
|
|
1275
|
-
|
|
1276
|
-
if (typeof data[key] !== 'undefined') {
|
|
1277
|
-
return data[key]
|
|
1278
|
-
}
|
|
1279
|
-
|
|
1280
|
-
return _handleFailover(key, failover)
|
|
1281
1300
|
}
|
|
1282
1301
|
|
|
1283
1302
|
function _handleFailover(key, failover) {
|
|
@@ -1292,7 +1311,11 @@ function _handleFailover(key, failover) {
|
|
|
1292
1311
|
|
|
1293
1312
|
;// ./lib/models/template/helpers/_join.js
|
|
1294
1313
|
function _join(data, delimiter) {
|
|
1295
|
-
|
|
1314
|
+
try {
|
|
1315
|
+
return data.join(delimiter)
|
|
1316
|
+
} catch (e) {
|
|
1317
|
+
throw e
|
|
1318
|
+
}
|
|
1296
1319
|
}
|
|
1297
1320
|
|
|
1298
1321
|
|
|
@@ -1302,41 +1325,46 @@ function _join(data, delimiter) {
|
|
|
1302
1325
|
|
|
1303
1326
|
|
|
1304
1327
|
function _map(data, args) {
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
}
|
|
1309
|
-
|
|
1310
|
-
const result = data.reduce((acc, item) => {
|
|
1311
|
-
if (args.length === 1 && Array.isArray(args[0])) {
|
|
1312
|
-
args = args[0]
|
|
1313
|
-
acc.hasFormat = true
|
|
1328
|
+
try {
|
|
1329
|
+
if (args.length === 0) {
|
|
1330
|
+
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
|
|
1314
1331
|
}
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1332
|
+
if (data === null || (typeof data === 'undefined')) {
|
|
1333
|
+
return null
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
const result = data.reduce((acc, item) => {
|
|
1337
|
+
if (args.length === 1 && Array.isArray(args[0])) {
|
|
1338
|
+
args = args[0]
|
|
1339
|
+
acc.hasFormat = true
|
|
1322
1340
|
}
|
|
1323
|
-
|
|
1324
|
-
|
|
1341
|
+
const list = args.map((key) => {
|
|
1342
|
+
if (key.includes('.')) {
|
|
1343
|
+
const parts = key.split('.')
|
|
1344
|
+
const first = parts[0]
|
|
1345
|
+
parts.shift()
|
|
1346
|
+
const remainingKey = parts.join('.')
|
|
1347
|
+
return _get(item[first], remainingKey)
|
|
1348
|
+
}
|
|
1349
|
+
if (typeof item[key] !== 'undefined') {
|
|
1350
|
+
return item[key]
|
|
1351
|
+
}
|
|
1352
|
+
return null
|
|
1353
|
+
})
|
|
1354
|
+
if (acc.hasFormat) {
|
|
1355
|
+
acc.content.push(list)
|
|
1356
|
+
} else {
|
|
1357
|
+
acc.content = acc.content.concat(list)
|
|
1325
1358
|
}
|
|
1326
|
-
return
|
|
1327
|
-
|
|
1359
|
+
return acc
|
|
1360
|
+
}, {
|
|
1361
|
+
content: [],
|
|
1362
|
+
hasFormat: false
|
|
1328
1363
|
})
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
}
|
|
1334
|
-
return acc
|
|
1335
|
-
}, {
|
|
1336
|
-
content: [],
|
|
1337
|
-
hasFormat: false
|
|
1338
|
-
})
|
|
1339
|
-
return result.content
|
|
1364
|
+
return result.content
|
|
1365
|
+
} catch (e) {
|
|
1366
|
+
throw e
|
|
1367
|
+
}
|
|
1340
1368
|
}
|
|
1341
1369
|
|
|
1342
1370
|
|
|
@@ -1381,12 +1409,16 @@ class Template {
|
|
|
1381
1409
|
const regex = new RegExp(`${this.delimiters[0]}\\s(.*?)\\s${this.delimiters[1]}`)
|
|
1382
1410
|
const match = expression.match(regex)
|
|
1383
1411
|
if (match !== null) {
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
return
|
|
1387
|
-
|
|
1412
|
+
try {
|
|
1413
|
+
const functionList = _parseFunction(match[1], _FN_NAMES)
|
|
1414
|
+
return functionList.reduce((acc, fn) => {
|
|
1415
|
+
return _callFunction(acc, fn.name, fn.args)
|
|
1416
|
+
}, this.data)
|
|
1417
|
+
} catch (e) {
|
|
1418
|
+
throw new TemplateException(`Template engine error: ${e.message}`)
|
|
1419
|
+
}
|
|
1388
1420
|
}
|
|
1389
|
-
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.
|
|
1421
|
+
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.invalidRegExpException)
|
|
1390
1422
|
}
|
|
1391
1423
|
}
|
|
1392
1424
|
|
|
@@ -1470,59 +1502,63 @@ function _parseSinglePart(input) {
|
|
|
1470
1502
|
}
|
|
1471
1503
|
|
|
1472
1504
|
function _callFunction(data, functionName, parameters) {
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1505
|
+
try {
|
|
1506
|
+
let failover
|
|
1507
|
+
switch (functionName) {
|
|
1508
|
+
case 'get':
|
|
1509
|
+
if (parameters.length > 2) {
|
|
1510
|
+
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentFormatException)
|
|
1511
|
+
}
|
|
1512
|
+
if (parameters.length === 2) {
|
|
1513
|
+
failover = parameters[parameters.length - 1]
|
|
1514
|
+
}
|
|
1515
|
+
return _get(data, parameters[0], failover)
|
|
1516
|
+
case 'map':
|
|
1517
|
+
return _map(data, parameters)
|
|
1518
|
+
case 'join':
|
|
1519
|
+
return _join(data, parameters[0])
|
|
1520
|
+
// case 'concatIf':
|
|
1521
|
+
// if (parameters.length != 3) {
|
|
1522
|
+
// throw const TemplateException(TemplateExceptionType.argumentFormatException);
|
|
1523
|
+
// }
|
|
1524
|
+
// var condition = parameters.first;
|
|
1525
|
+
// var success = parameters[1];
|
|
1526
|
+
// var failover = parameters[2];
|
|
1527
|
+
// return _concatIf(data, condition, success, failover);
|
|
1528
|
+
case 'filterOne':
|
|
1529
|
+
return _filterOne(data, parameters)
|
|
1530
|
+
case 'filterAll':
|
|
1531
|
+
return _filterAll(data, parameters)
|
|
1532
|
+
// case 'formatDate':
|
|
1533
|
+
// if (parameters is List<String>) {
|
|
1534
|
+
// return _formatDate(data, parameters);
|
|
1535
|
+
// }
|
|
1536
|
+
// throw const TemplateException(TemplateExceptionType.argumentFormatException);
|
|
1537
|
+
case 'eq':
|
|
1538
|
+
return _eq(data, parameters)
|
|
1539
|
+
// case 'neq':
|
|
1540
|
+
// return _neq(data, parameters);
|
|
1541
|
+
// case 'gt':
|
|
1542
|
+
// return _gt(data, parameters);
|
|
1543
|
+
// case 'gte':
|
|
1544
|
+
// return _gte(data, parameters);
|
|
1545
|
+
// case 'lt':
|
|
1546
|
+
// return _lt(data, parameters);
|
|
1547
|
+
// case 'lte':
|
|
1548
|
+
// return _lte(data, parameters);
|
|
1549
|
+
// case 'isEmpty':
|
|
1550
|
+
// return _isEmpty(data, parameters);
|
|
1551
|
+
// case 'isNotEmpty':
|
|
1552
|
+
// return _isNotEmpty(data, parameters);
|
|
1553
|
+
// case 'toLowerCase':
|
|
1554
|
+
// return _toLowerCase(data);
|
|
1555
|
+
// case 'toUpperCase':
|
|
1556
|
+
// return _toUpperCase(data);
|
|
1557
|
+
default:
|
|
1558
|
+
throw new Error(`${functionName} is not a valid function`)
|
|
1559
|
+
}
|
|
1560
|
+
} catch (e) {
|
|
1561
|
+
throw e
|
|
1526
1562
|
}
|
|
1527
1563
|
}
|
|
1528
1564
|
|
package/dist/index.min.js
CHANGED
|
@@ -936,11 +936,11 @@ class Repo {
|
|
|
936
936
|
reject(new Error('more than one is found'))
|
|
937
937
|
}
|
|
938
938
|
})
|
|
939
|
-
.catch((err) => {
|
|
940
|
-
log({ level: 'warn', output: err.toString() })
|
|
941
|
-
throw err
|
|
942
|
-
})
|
|
943
939
|
})
|
|
940
|
+
.catch((err) => {
|
|
941
|
+
log({ level: 'warn', output: err.toString() })
|
|
942
|
+
throw err
|
|
943
|
+
})
|
|
944
944
|
}
|
|
945
945
|
|
|
946
946
|
saveAll({ docs, systemLog }) {
|
|
@@ -954,14 +954,14 @@ class Repo {
|
|
|
954
954
|
const promise = typeof this.model.saveAll === 'function'
|
|
955
955
|
? this.model.saveAll({ docs })
|
|
956
956
|
: Promise.all(docs.map(async (doc) => {
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
957
|
+
if (doc) {
|
|
958
|
+
const result = await this.saveOne({ doc })
|
|
959
|
+
isNew = result.isNew
|
|
960
|
+
const _data = result._data || result.data
|
|
961
|
+
return _data[0]
|
|
962
|
+
}
|
|
963
|
+
return null
|
|
964
|
+
}))
|
|
965
965
|
return promise.then((savedData) => {
|
|
966
966
|
if (savedData.length !== 1) isNew = null
|
|
967
967
|
const result = {
|
|
@@ -1121,6 +1121,27 @@ function makeService({ repo }) {
|
|
|
1121
1121
|
|
|
1122
1122
|
|
|
1123
1123
|
|
|
1124
|
+
;// ./lib/models/template/templateException.js
|
|
1125
|
+
const TEMPLATE_EXCEPTION_TYPE = {
|
|
1126
|
+
argumentEmptyException: 'Argument is empty',
|
|
1127
|
+
argumentFormatException: 'Incorrect number or format of argument',
|
|
1128
|
+
invalidFuntionException: 'Function Name is invalid',
|
|
1129
|
+
invalidRegExpException: 'Invalid regular expression',
|
|
1130
|
+
isNotAFunctionException: 'Is not a function',
|
|
1131
|
+
notExistException: 'Key does not exist',
|
|
1132
|
+
resultEmptyException: 'Result is empty',
|
|
1133
|
+
resultMoreThanOneException: 'More than one result'
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
class TemplateException extends Error {
|
|
1137
|
+
constructor(message) {
|
|
1138
|
+
super(message)
|
|
1139
|
+
this.message = message
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
|
|
1124
1145
|
;// ./lib/models/template/constants.js
|
|
1125
1146
|
const _EMPTY = '_EMPTY'
|
|
1126
1147
|
const _FN_NAMES = [
|
|
@@ -1137,8 +1158,13 @@ const TAGS_HANDLEBAR = ['{{', '}}']
|
|
|
1137
1158
|
;// ./lib/models/template/helpers/_eq.js
|
|
1138
1159
|
|
|
1139
1160
|
|
|
1161
|
+
|
|
1162
|
+
|
|
1140
1163
|
function _eq(data, args) {
|
|
1141
|
-
if (
|
|
1164
|
+
if (args.length !== 3) {
|
|
1165
|
+
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentFormatException)
|
|
1166
|
+
}
|
|
1167
|
+
if (data === null || (typeof data === 'undefined')) {
|
|
1142
1168
|
return null
|
|
1143
1169
|
}
|
|
1144
1170
|
if (args.includes(_SELF)) {
|
|
@@ -1152,46 +1178,29 @@ function _eq(data, args) {
|
|
|
1152
1178
|
|
|
1153
1179
|
|
|
1154
1180
|
|
|
1155
|
-
;// ./lib/models/template/templateException.js
|
|
1156
|
-
const TEMPLATE_EXCEPTION_TYPE = {
|
|
1157
|
-
argumentEmptyException: 'Argument is empty',
|
|
1158
|
-
argumentFormatException: 'Incorrect number or format of argument',
|
|
1159
|
-
invalidFuntionException: 'Function Name is invalid',
|
|
1160
|
-
invalidRegExpException: 'Invalid regular expression',
|
|
1161
|
-
isNotAFunctionException: 'Is not a function',
|
|
1162
|
-
notExistException: 'Key does not exist',
|
|
1163
|
-
resultEmptyException: 'Result is empty',
|
|
1164
|
-
resultMoreThanOneException: 'More than one result'
|
|
1165
|
-
}
|
|
1166
|
-
|
|
1167
|
-
class TemplateException extends Error {
|
|
1168
|
-
constructor(message) {
|
|
1169
|
-
super(message)
|
|
1170
|
-
this.message = message
|
|
1171
|
-
}
|
|
1172
|
-
}
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
1181
|
;// ./lib/models/template/helpers/_filterAll.js
|
|
1177
1182
|
|
|
1178
1183
|
|
|
1179
1184
|
const _filterAll_DELIMITER = '~~~'
|
|
1180
1185
|
|
|
1181
1186
|
function _filterAll(data, args) {
|
|
1182
|
-
|
|
1187
|
+
try {
|
|
1188
|
+
if (!Array.isArray(args) || args.length === 0) {
|
|
1189
|
+
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
|
|
1190
|
+
}
|
|
1191
|
+
if (!Array.isArray(data) || data.length === 0) {
|
|
1192
|
+
return []
|
|
1193
|
+
}
|
|
1194
|
+
if (typeof data[0] === 'object') {
|
|
1195
|
+
return _existObject(data, args)
|
|
1196
|
+
}
|
|
1197
|
+
if (typeof data[0] === 'string' || typeof data[0] === 'number') {
|
|
1198
|
+
return _exist(data, args)
|
|
1199
|
+
}
|
|
1183
1200
|
return []
|
|
1201
|
+
} catch (e) {
|
|
1202
|
+
throw e
|
|
1184
1203
|
}
|
|
1185
|
-
|
|
1186
|
-
if (typeof data[0] === 'object') {
|
|
1187
|
-
return _existObject(data, args)
|
|
1188
|
-
}
|
|
1189
|
-
|
|
1190
|
-
if (typeof data[0] === 'string' || typeof data[0] === 'number') {
|
|
1191
|
-
return _exist(data, args)
|
|
1192
|
-
}
|
|
1193
|
-
|
|
1194
|
-
return []
|
|
1195
1204
|
}
|
|
1196
1205
|
|
|
1197
1206
|
function _exist(data, args) {
|
|
@@ -1218,11 +1227,18 @@ function _existObject(data, args) {
|
|
|
1218
1227
|
|
|
1219
1228
|
|
|
1220
1229
|
function _filterOne(data, args) {
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1230
|
+
try {
|
|
1231
|
+
const list = _filterAll(data, args)
|
|
1232
|
+
if (list.length === 1) {
|
|
1233
|
+
return list[0]
|
|
1234
|
+
}
|
|
1235
|
+
if (list.length === 0) {
|
|
1236
|
+
return null
|
|
1237
|
+
}
|
|
1238
|
+
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.resultMoreThanOneException)
|
|
1239
|
+
} catch (e) {
|
|
1240
|
+
throw e
|
|
1224
1241
|
}
|
|
1225
|
-
return null
|
|
1226
1242
|
}
|
|
1227
1243
|
|
|
1228
1244
|
|
|
@@ -1231,28 +1247,31 @@ function _filterOne(data, args) {
|
|
|
1231
1247
|
|
|
1232
1248
|
|
|
1233
1249
|
function _get(data, key, failover = null) {
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
if (
|
|
1242
|
-
const
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1250
|
+
try {
|
|
1251
|
+
if (key === null || (typeof key === 'undefined') || key === '') {
|
|
1252
|
+
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
|
|
1253
|
+
}
|
|
1254
|
+
if (data === null) {
|
|
1255
|
+
return null
|
|
1256
|
+
}
|
|
1257
|
+
if (key.includes('.')) {
|
|
1258
|
+
const parts = key.split('.')
|
|
1259
|
+
if (parts.length > 1) {
|
|
1260
|
+
const first = parts.shift()
|
|
1261
|
+
const remainingKey = parts.join('.')
|
|
1262
|
+
if (typeof data[first] !== 'undefined') {
|
|
1263
|
+
return _get(data[first], remainingKey, failover)
|
|
1264
|
+
}
|
|
1265
|
+
return _handleFailover(key, failover)
|
|
1246
1266
|
}
|
|
1247
|
-
return _handleFailover(key, failover)
|
|
1248
1267
|
}
|
|
1268
|
+
if (typeof data[key] !== 'undefined') {
|
|
1269
|
+
return data[key]
|
|
1270
|
+
}
|
|
1271
|
+
return _handleFailover(key, failover)
|
|
1272
|
+
} catch (e) {
|
|
1273
|
+
throw e
|
|
1249
1274
|
}
|
|
1250
|
-
|
|
1251
|
-
if (typeof data[key] !== 'undefined') {
|
|
1252
|
-
return data[key]
|
|
1253
|
-
}
|
|
1254
|
-
|
|
1255
|
-
return _handleFailover(key, failover)
|
|
1256
1275
|
}
|
|
1257
1276
|
|
|
1258
1277
|
function _handleFailover(key, failover) {
|
|
@@ -1267,7 +1286,11 @@ function _handleFailover(key, failover) {
|
|
|
1267
1286
|
|
|
1268
1287
|
;// ./lib/models/template/helpers/_join.js
|
|
1269
1288
|
function _join(data, delimiter) {
|
|
1270
|
-
|
|
1289
|
+
try {
|
|
1290
|
+
return data.join(delimiter)
|
|
1291
|
+
} catch (e) {
|
|
1292
|
+
throw e
|
|
1293
|
+
}
|
|
1271
1294
|
}
|
|
1272
1295
|
|
|
1273
1296
|
|
|
@@ -1277,41 +1300,46 @@ function _join(data, delimiter) {
|
|
|
1277
1300
|
|
|
1278
1301
|
|
|
1279
1302
|
function _map(data, args) {
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
}
|
|
1284
|
-
|
|
1285
|
-
const result = data.reduce((acc, item) => {
|
|
1286
|
-
if (args.length === 1 && Array.isArray(args[0])) {
|
|
1287
|
-
args = args[0]
|
|
1288
|
-
acc.hasFormat = true
|
|
1303
|
+
try {
|
|
1304
|
+
if (args.length === 0) {
|
|
1305
|
+
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
|
|
1289
1306
|
}
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1307
|
+
if (data === null || (typeof data === 'undefined')) {
|
|
1308
|
+
return null
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
const result = data.reduce((acc, item) => {
|
|
1312
|
+
if (args.length === 1 && Array.isArray(args[0])) {
|
|
1313
|
+
args = args[0]
|
|
1314
|
+
acc.hasFormat = true
|
|
1297
1315
|
}
|
|
1298
|
-
|
|
1299
|
-
|
|
1316
|
+
const list = args.map((key) => {
|
|
1317
|
+
if (key.includes('.')) {
|
|
1318
|
+
const parts = key.split('.')
|
|
1319
|
+
const first = parts[0]
|
|
1320
|
+
parts.shift()
|
|
1321
|
+
const remainingKey = parts.join('.')
|
|
1322
|
+
return _get(item[first], remainingKey)
|
|
1323
|
+
}
|
|
1324
|
+
if (typeof item[key] !== 'undefined') {
|
|
1325
|
+
return item[key]
|
|
1326
|
+
}
|
|
1327
|
+
return null
|
|
1328
|
+
})
|
|
1329
|
+
if (acc.hasFormat) {
|
|
1330
|
+
acc.content.push(list)
|
|
1331
|
+
} else {
|
|
1332
|
+
acc.content = acc.content.concat(list)
|
|
1300
1333
|
}
|
|
1301
|
-
return
|
|
1302
|
-
|
|
1334
|
+
return acc
|
|
1335
|
+
}, {
|
|
1336
|
+
content: [],
|
|
1337
|
+
hasFormat: false
|
|
1303
1338
|
})
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
}
|
|
1309
|
-
return acc
|
|
1310
|
-
}, {
|
|
1311
|
-
content: [],
|
|
1312
|
-
hasFormat: false
|
|
1313
|
-
})
|
|
1314
|
-
return result.content
|
|
1339
|
+
return result.content
|
|
1340
|
+
} catch (e) {
|
|
1341
|
+
throw e
|
|
1342
|
+
}
|
|
1315
1343
|
}
|
|
1316
1344
|
|
|
1317
1345
|
|
|
@@ -1356,12 +1384,16 @@ class Template {
|
|
|
1356
1384
|
const regex = new RegExp(`${this.delimiters[0]}\\s(.*?)\\s${this.delimiters[1]}`)
|
|
1357
1385
|
const match = expression.match(regex)
|
|
1358
1386
|
if (match !== null) {
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
return
|
|
1362
|
-
|
|
1387
|
+
try {
|
|
1388
|
+
const functionList = _parseFunction(match[1], _FN_NAMES)
|
|
1389
|
+
return functionList.reduce((acc, fn) => {
|
|
1390
|
+
return _callFunction(acc, fn.name, fn.args)
|
|
1391
|
+
}, this.data)
|
|
1392
|
+
} catch (e) {
|
|
1393
|
+
throw new TemplateException(`Template engine error: ${e.message}`)
|
|
1394
|
+
}
|
|
1363
1395
|
}
|
|
1364
|
-
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.
|
|
1396
|
+
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.invalidRegExpException)
|
|
1365
1397
|
}
|
|
1366
1398
|
}
|
|
1367
1399
|
|
|
@@ -1445,59 +1477,63 @@ function _parseSinglePart(input) {
|
|
|
1445
1477
|
}
|
|
1446
1478
|
|
|
1447
1479
|
function _callFunction(data, functionName, parameters) {
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1480
|
+
try {
|
|
1481
|
+
let failover
|
|
1482
|
+
switch (functionName) {
|
|
1483
|
+
case 'get':
|
|
1484
|
+
if (parameters.length > 2) {
|
|
1485
|
+
throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentFormatException)
|
|
1486
|
+
}
|
|
1487
|
+
if (parameters.length === 2) {
|
|
1488
|
+
failover = parameters[parameters.length - 1]
|
|
1489
|
+
}
|
|
1490
|
+
return _get(data, parameters[0], failover)
|
|
1491
|
+
case 'map':
|
|
1492
|
+
return _map(data, parameters)
|
|
1493
|
+
case 'join':
|
|
1494
|
+
return _join(data, parameters[0])
|
|
1495
|
+
// case 'concatIf':
|
|
1496
|
+
// if (parameters.length != 3) {
|
|
1497
|
+
// throw const TemplateException(TemplateExceptionType.argumentFormatException);
|
|
1498
|
+
// }
|
|
1499
|
+
// var condition = parameters.first;
|
|
1500
|
+
// var success = parameters[1];
|
|
1501
|
+
// var failover = parameters[2];
|
|
1502
|
+
// return _concatIf(data, condition, success, failover);
|
|
1503
|
+
case 'filterOne':
|
|
1504
|
+
return _filterOne(data, parameters)
|
|
1505
|
+
case 'filterAll':
|
|
1506
|
+
return _filterAll(data, parameters)
|
|
1507
|
+
// case 'formatDate':
|
|
1508
|
+
// if (parameters is List<String>) {
|
|
1509
|
+
// return _formatDate(data, parameters);
|
|
1510
|
+
// }
|
|
1511
|
+
// throw const TemplateException(TemplateExceptionType.argumentFormatException);
|
|
1512
|
+
case 'eq':
|
|
1513
|
+
return _eq(data, parameters)
|
|
1514
|
+
// case 'neq':
|
|
1515
|
+
// return _neq(data, parameters);
|
|
1516
|
+
// case 'gt':
|
|
1517
|
+
// return _gt(data, parameters);
|
|
1518
|
+
// case 'gte':
|
|
1519
|
+
// return _gte(data, parameters);
|
|
1520
|
+
// case 'lt':
|
|
1521
|
+
// return _lt(data, parameters);
|
|
1522
|
+
// case 'lte':
|
|
1523
|
+
// return _lte(data, parameters);
|
|
1524
|
+
// case 'isEmpty':
|
|
1525
|
+
// return _isEmpty(data, parameters);
|
|
1526
|
+
// case 'isNotEmpty':
|
|
1527
|
+
// return _isNotEmpty(data, parameters);
|
|
1528
|
+
// case 'toLowerCase':
|
|
1529
|
+
// return _toLowerCase(data);
|
|
1530
|
+
// case 'toUpperCase':
|
|
1531
|
+
// return _toUpperCase(data);
|
|
1532
|
+
default:
|
|
1533
|
+
throw new Error(`${functionName} is not a valid function`)
|
|
1534
|
+
}
|
|
1535
|
+
} catch (e) {
|
|
1536
|
+
throw e
|
|
1501
1537
|
}
|
|
1502
1538
|
}
|
|
1503
1539
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@questwork/q-utilities",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "Questwork QUtilities",
|
|
5
5
|
"main": "dist/index.min.js",
|
|
6
6
|
"type": "module",
|
|
@@ -10,6 +10,11 @@
|
|
|
10
10
|
"default": "./dist/index.min.js"
|
|
11
11
|
}
|
|
12
12
|
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "cross-env NODE_ENV=production minimize=false gulp",
|
|
15
|
+
"lint": "eslint .",
|
|
16
|
+
"test:models": "NODE_ENV=test mocha --exit 'lib/models/test.setup.js' 'lib/models/**/*.spec.js'"
|
|
17
|
+
},
|
|
13
18
|
"author": {
|
|
14
19
|
"name": "Questwork Consulting Limited",
|
|
15
20
|
"email": "info@questwork.com",
|
|
@@ -40,10 +45,5 @@
|
|
|
40
45
|
},
|
|
41
46
|
"engines": {
|
|
42
47
|
"node": ">=10.0.0"
|
|
43
|
-
},
|
|
44
|
-
"scripts": {
|
|
45
|
-
"build": "cross-env NODE_ENV=production minimize=false gulp",
|
|
46
|
-
"lint": "eslint .",
|
|
47
|
-
"test:models": "NODE_ENV=test mocha --exit 'lib/models/test.setup.js' 'lib/models/**/*.spec.js'"
|
|
48
48
|
}
|
|
49
|
-
}
|
|
49
|
+
}
|