@questwork/q-utilities 0.1.9 → 0.1.10

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.js CHANGED
@@ -30,7 +30,7 @@ __webpack_require__.d(__webpack_exports__, {
30
30
  Z8: () => (/* reexport */ QMeta),
31
31
  lc: () => (/* reexport */ Repo),
32
32
  kl: () => (/* reexport */ Service),
33
- Bj: () => (/* reexport */ Template),
33
+ Mg: () => (/* reexport */ TemplateCompiler),
34
34
  _x: () => (/* reexport */ UniqueKeyGenerator),
35
35
  l0: () => (/* reexport */ convertString),
36
36
  Yq: () => (/* reexport */ formatDate),
@@ -207,6 +207,16 @@ function getValidation(rule, data, getDataByKey, KeyValueObject) {
207
207
  }
208
208
  return false
209
209
  }
210
+ case '$intervalTimeGt': {
211
+ const now = new Date().getTime()
212
+ const timestamp = new Date(rowValue).getTime()
213
+ return (now - timestamp) > value['$intervalTimeGt']
214
+ }
215
+ case '$intervalTimeLt': {
216
+ const now = new Date().getTime()
217
+ const timestamp = new Date(rowValue).getTime()
218
+ return (now - timestamp) < value['$intervalTimeLt']
219
+ }
210
220
  case '$notInValue': {
211
221
  const result = getDataByKey(value['$notInValue'], data)
212
222
  const _value = Array.isArray(result) ? result : []
@@ -1121,8 +1131,8 @@ function makeService({ repo }) {
1121
1131
 
1122
1132
 
1123
1133
 
1124
- ;// ./lib/models/template/templateException.js
1125
- const TEMPLATE_EXCEPTION_TYPE = {
1134
+ ;// ./lib/models/templateCompiler/templateCompilerException.js
1135
+ const TEMPLATE_COMPILER_EXCEPTION_TYPE = {
1126
1136
  argumentEmptyException: 'Argument is empty',
1127
1137
  argumentFormatException: 'Incorrect number or format of argument',
1128
1138
  invalidFuntionException: 'Function Name is invalid',
@@ -1133,7 +1143,7 @@ const TEMPLATE_EXCEPTION_TYPE = {
1133
1143
  resultMoreThanOneException: 'More than one result'
1134
1144
  }
1135
1145
 
1136
- class TemplateException extends Error {
1146
+ class TemplateCompilerException extends Error {
1137
1147
  constructor(message) {
1138
1148
  super(message)
1139
1149
  this.message = message
@@ -1142,10 +1152,10 @@ class TemplateException extends Error {
1142
1152
 
1143
1153
 
1144
1154
 
1145
- ;// ./lib/models/template/constants.js
1155
+ ;// ./lib/models/templateCompiler/constants.js
1146
1156
  const _EMPTY = '_EMPTY'
1147
1157
  const _FN_NAMES = [
1148
- 'get', 'map', 'join', 'concatIf', 'filterOne', 'filterAll', 'formatDate', 'eq', 'neq', 'gt', 'lt', 'gte', 'lte', 'isEmpty', 'isNotEmpty', 'toLowerCase', 'toUpperCase'
1158
+ 'get', 'map', 'join', 'concatIf', 'exec', 'filterOne', 'filterAll', 'formatDate', 'eq', 'neq', 'gt', 'lt', 'gte', 'lte', 'isEmpty', 'isNotEmpty', 'toLowerCase', 'toUpperCase'
1149
1159
  ]
1150
1160
  const _HIDE = '_HIDE'
1151
1161
  const _NOT_EMPTY = '_NOT_EMPTY'
@@ -1155,14 +1165,45 @@ const TAGS_HANDLEBAR = ['{{', '}}']
1155
1165
 
1156
1166
 
1157
1167
 
1158
- ;// ./lib/models/template/helpers/_eq.js
1168
+ ;// ./lib/models/templateCompiler/helpers/_concatIf.js
1169
+
1170
+
1171
+
1172
+
1173
+ function _concatIf(data, args) {
1174
+ if (typeof data !== 'string') {
1175
+ throw new TemplateCompilerException(`_concatIf: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: the data must be string :${data.join(', ')}`)
1176
+ }
1177
+ if (args.length !== 3) {
1178
+ throw new TemplateCompilerException(`_concatIf: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1179
+ }
1180
+ if (data === null || (typeof data === 'undefined')) {
1181
+ return null
1182
+ }
1183
+ const [condition, success, failover] = args
1184
+ const validConditions = [_EMPTY, _NOT_EMPTY]
1185
+ if (validConditions.includes(condition) || success.length !== 2) {
1186
+ throw new TemplateCompilerException(`concatIf: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentEmptyException}: ${condition}, ${success}`)
1187
+ }
1188
+ if (data === '' && failover.includes(_HIDE)) {
1189
+ return ''
1190
+ }
1191
+ if (data !== '' && (data !== null || data !== undefined) && failover.includes(_HIDE)) {
1192
+ return `${success[0]}${data}${success[success.length - 1]}`
1193
+ }
1194
+ return failover
1195
+ }
1196
+
1197
+
1198
+
1199
+ ;// ./lib/models/templateCompiler/helpers/_eq.js
1159
1200
 
1160
1201
 
1161
1202
 
1162
1203
 
1163
1204
  function _eq(data, args) {
1164
1205
  if (args.length !== 3) {
1165
- throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentFormatException)
1206
+ throw new TemplateCompilerException(`eq: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1166
1207
  }
1167
1208
  if (data === null || (typeof data === 'undefined')) {
1168
1209
  return null
@@ -1178,15 +1219,28 @@ function _eq(data, args) {
1178
1219
 
1179
1220
 
1180
1221
 
1181
- ;// ./lib/models/template/helpers/_filterAll.js
1222
+ ;// ./lib/models/templateCompiler/helpers/_exec.js
1223
+ function _exec(data, args) {
1224
+ try {
1225
+ const [methodName, ..._args] = args
1226
+ return data[methodName](..._args)
1227
+ } catch (e) {
1228
+ throw e
1229
+ }
1230
+ }
1182
1231
 
1183
1232
 
1184
- const _filterAll_DELIMITER = '~~~'
1233
+
1234
+ ;// ./lib/models/templateCompiler/helpers/_filterAll.js
1235
+
1236
+
1237
+
1238
+ // const DELIMITER = '~~~'
1185
1239
 
1186
1240
  function _filterAll(data, args) {
1187
1241
  try {
1188
1242
  if (!Array.isArray(args) || args.length === 0) {
1189
- throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1243
+ throw new TemplateCompilerException(TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentEmptyException)
1190
1244
  }
1191
1245
  if (!Array.isArray(data) || data.length === 0) {
1192
1246
  return []
@@ -1204,25 +1258,140 @@ function _filterAll(data, args) {
1204
1258
  }
1205
1259
 
1206
1260
  function _exist(data, args) {
1207
- const _str = `${_filterAll_DELIMITER}${args.join(_filterAll_DELIMITER)}${_filterAll_DELIMITER}`
1208
- return data.filter((e) => {
1209
- return _str.includes(`${_filterAll_DELIMITER}${e}${_filterAll_DELIMITER}`) && args.includes(e)
1210
- })
1261
+ const _args = args.flat()
1262
+ return data.filter((e) => _args.some((arg) => _performOperation(arg, e)))
1211
1263
  }
1212
1264
 
1213
1265
  function _existObject(data, args) {
1214
- if (args.length !== 2) {
1215
- return []
1266
+ if (args.length === 1) {
1267
+ const arg = args[0]
1268
+ return data.filter((e) => {
1269
+ if (arg.includes('.')) {
1270
+ return getValueByKeys(arg.split('.'), e)
1271
+ }
1272
+ return Object.prototype.hasOwnProperty.call(e, arg)
1273
+ })
1216
1274
  }
1217
- const [key, _args] = args
1275
+
1276
+ if (args.length > 2) {
1277
+ let res = data
1278
+ for (let i = 0; i < args.length; i += 2) {
1279
+ const group = [args[i], args[i + 1]]
1280
+ res = _existObject(res, group)
1281
+ }
1282
+ return res
1283
+ }
1284
+
1285
+ const [key, ..._argsArr] = args
1286
+ const _args = _argsArr.flat()
1218
1287
  return data.filter((e) => {
1219
- return _args.includes(e[key])
1288
+ const value = key.includes('.') ? getValueByKeys(key.split('.'), e) : e[key]
1289
+ return _args.some((arg) => _performOperation(arg, value))
1220
1290
  })
1221
1291
  }
1222
1292
 
1293
+ function _performOperation(arg, value) {
1294
+ // the arg is undefined
1295
+ if (arg === undefined && value === undefined) return true
1223
1296
 
1297
+ // the arg is null
1298
+ if (arg === null && value === null) return true
1224
1299
 
1225
- ;// ./lib/models/template/helpers/_filterOne.js
1300
+ // the arg is boolean
1301
+ if (typeof arg === 'boolean') {
1302
+ return arg === value
1303
+ }
1304
+
1305
+ // the arg is blank or *: Blank => Empty, * => Not Empty
1306
+ if (arg === '' || arg === '*') {
1307
+ // null and undefined are not included in either case
1308
+ if (value === null || value === undefined) {
1309
+ return false
1310
+ }
1311
+ if (typeof value === 'string') {
1312
+ return arg === '' ? value === '' : value !== ''
1313
+ }
1314
+ if (Array.isArray(value)) {
1315
+ return arg === '' ? value.length === 0 : value.length !== 0
1316
+ }
1317
+ return arg !== ''
1318
+ }
1319
+
1320
+ // the arg is alphabetic or number
1321
+ if (_isPureStringOrNumber(arg)) {
1322
+ return arg === value
1323
+ }
1324
+
1325
+ // the arg is array of [] or [*]: [] => Empty, [*] => Not Empty
1326
+ if (arg.startsWith('[') && arg.endsWith(']')) {
1327
+ if (arg === '[]') {
1328
+ return Array.isArray(value) && value.length === 0
1329
+ }
1330
+ if (arg === '[*]') {
1331
+ return Array.isArray(value) && value.length !== 0
1332
+ }
1333
+ return false
1334
+ }
1335
+
1336
+ // the arg is 'operator + string | number'
1337
+ const { operator, value: argValue } = _splitOperator(arg)
1338
+ if (!operator || (argValue !== 0 && !argValue)) {
1339
+ return false
1340
+ }
1341
+ switch (operator) {
1342
+ case '>':
1343
+ return value > argValue
1344
+ case '<':
1345
+ return value < argValue
1346
+ case '!=':
1347
+ return value !== argValue
1348
+ case '>=':
1349
+ return value >= argValue
1350
+ case '<=':
1351
+ return value <= argValue
1352
+ default:
1353
+ return false
1354
+ }
1355
+ }
1356
+
1357
+ function _isPureStringOrNumber(input) {
1358
+ if (typeof input === 'string') {
1359
+ if (input.startsWith('[') && input.endsWith(']')) {
1360
+ return false
1361
+ }
1362
+ if (/!=|>=|<=|>|</.test(input)) {
1363
+ return false
1364
+ }
1365
+ return true
1366
+ }
1367
+ return !Number.isNaN(input)
1368
+ }
1369
+
1370
+ function _splitOperator(str) {
1371
+ const operators = ['!=', '>=', '<=', '>', '<']
1372
+
1373
+ const matchedOp = operators.find((op) => str.startsWith(op))
1374
+ if (!matchedOp) return { operator: null, value: null }
1375
+
1376
+ const remaining = str.slice(matchedOp.length)
1377
+
1378
+ // '>Primary' or '<Primary' is invalid
1379
+ if (/^[a-zA-Z]*$/.test(remaining) && matchedOp !== '!=') {
1380
+ return { operator: null, value: null }
1381
+ }
1382
+
1383
+ // if it is a number it is converted to a number
1384
+ const value = (!Number.isNaN(parseFloat(remaining)) && !Number.isNaN(remaining)) ? Number(remaining) : remaining
1385
+
1386
+ return {
1387
+ operator: matchedOp,
1388
+ value
1389
+ }
1390
+ }
1391
+
1392
+
1393
+
1394
+ ;// ./lib/models/templateCompiler/helpers/_filterOne.js
1226
1395
 
1227
1396
 
1228
1397
 
@@ -1235,7 +1404,7 @@ function _filterOne(data, args) {
1235
1404
  if (list.length === 0) {
1236
1405
  return null
1237
1406
  }
1238
- throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.resultMoreThanOneException)
1407
+ throw new TemplateCompilerException(TEMPLATE_COMPILER_EXCEPTION_TYPE.resultMoreThanOneException)
1239
1408
  } catch (e) {
1240
1409
  throw e
1241
1410
  }
@@ -1243,13 +1412,49 @@ function _filterOne(data, args) {
1243
1412
 
1244
1413
 
1245
1414
 
1246
- ;// ./lib/models/template/helpers/_get.js
1415
+ ;// ./lib/models/templateCompiler/helpers/_formatDate.js
1416
+
1417
+
1418
+ function _formatDate(timestamp, format) {
1419
+ if (format.length === 0) {
1420
+ throw new TemplateCompilerException(`_formateDate: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: format parts must be not empty array`)
1421
+ }
1422
+
1423
+ if (timestamp === null || timestamp === undefined) {
1424
+ return null
1425
+ }
1426
+
1427
+ const date = new Date(timestamp)
1428
+
1429
+ const partsMap = {
1430
+ yyyy: String(date.getFullYear()),
1431
+ mm: String(date.getMonth() + 1).padStart(2, '0'),
1432
+ dd: String(date.getDate()).padStart(2, '0')
1433
+ }
1434
+
1435
+ // Check for invalid format tokens
1436
+ const validTokens = ['yyyy', 'mm', 'dd']
1437
+ const invalidTokens = format.filter((part) => part.length > 1 && !validTokens.includes(part))
1438
+
1439
+ if (invalidTokens.length > 0) {
1440
+ throw new TemplateCompilerException(
1441
+ `_formateDate: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: the format type is not valid: ${format.join(', ')}`
1442
+ )
1443
+ }
1444
+
1445
+ // Build the formatted string using reduce
1446
+ return format.reduce((result, part) => result + (partsMap[part] || part), '')
1447
+ }
1448
+
1449
+
1450
+
1451
+ ;// ./lib/models/templateCompiler/helpers/_get.js
1247
1452
 
1248
1453
 
1249
1454
  function _get(data, key, failover = null) {
1250
1455
  try {
1251
1456
  if (key === null || (typeof key === 'undefined') || key === '') {
1252
- throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1457
+ throw new TemplateCompilerException(TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentEmptyException)
1253
1458
  }
1254
1459
  if (data === null) {
1255
1460
  return null
@@ -1279,30 +1484,186 @@ function _handleFailover(key, failover) {
1279
1484
  return failover
1280
1485
  }
1281
1486
  return null
1282
- // throw new TemplateException(`Key "${key}" does not exist and no failover`)
1487
+ // throw new TemplateCompilerException(`Key "${key}" does not exist and no failover`)
1488
+ }
1489
+
1490
+
1491
+
1492
+ ;// ./lib/models/templateCompiler/helpers/_gt.js
1493
+
1494
+
1495
+
1496
+
1497
+ function _gt(data, args) {
1498
+ if (args.length !== 3) {
1499
+ throw new TemplateCompilerException(`_gt: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1500
+ }
1501
+ if (data === null || (typeof data === 'undefined')) {
1502
+ return null
1503
+ }
1504
+ if (args.includes(_SELF)) {
1505
+ args = args.map((arg) => {
1506
+ return (arg === _SELF) ? data : arg
1507
+ })
1508
+ }
1509
+ const expected = args[0]
1510
+ return data > expected ? args[1] : args[2]
1511
+ }
1512
+
1513
+
1514
+
1515
+ ;// ./lib/models/templateCompiler/helpers/_gte.js
1516
+
1517
+
1518
+
1519
+
1520
+ function _gte(data, args) {
1521
+ if (args.length !== 3) {
1522
+ throw new TemplateCompilerException(`_gte: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1523
+ }
1524
+ if (data === null || (typeof data === 'undefined')) {
1525
+ return null
1526
+ }
1527
+ if (args.includes(_SELF)) {
1528
+ args = args.map((arg) => {
1529
+ return (arg === _SELF) ? data : arg
1530
+ })
1531
+ }
1532
+ const expected = args[0]
1533
+ return data >= expected ? args[1] : args[2]
1534
+ }
1535
+
1536
+
1537
+
1538
+ ;// ./lib/models/templateCompiler/helpers/_isEmpty.js
1539
+
1540
+
1541
+
1542
+
1543
+ function _isEmpty(data, args) {
1544
+ if (args.length !== 2) {
1545
+ throw new TemplateCompilerException(`_isEmpty: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1546
+ }
1547
+ // if (data === null || (typeof data === 'undefined')) {
1548
+ // return null
1549
+ // }
1550
+ if (args.includes(_SELF)) {
1551
+ args = args.map((arg) => {
1552
+ return (arg === _SELF) ? data : arg
1553
+ })
1554
+ }
1555
+ if (data !== null && typeof data === 'object' && Object.keys(data).length === 0) {
1556
+ return args[0]
1557
+ }
1558
+ return (data === '' || data === null || data === undefined || data.length === 0) ? args[0] : args[1]
1283
1559
  }
1284
1560
 
1285
1561
 
1286
1562
 
1287
- ;// ./lib/models/template/helpers/_join.js
1563
+ ;// ./lib/models/templateCompiler/helpers/_isNotEmpty.js
1564
+
1565
+
1566
+
1567
+
1568
+ function _isNotEmpty(data, args) {
1569
+ if (args.length !== 2) {
1570
+ throw new TemplateCompilerException(`_isNotEmpty: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1571
+ }
1572
+ // if (data === null || (typeof data === 'undefined')) {
1573
+ // return null
1574
+ // }
1575
+ if (args.includes(_SELF)) {
1576
+ args = args.map((arg) => {
1577
+ return (arg === _SELF) ? data : arg
1578
+ })
1579
+ }
1580
+ if (data !== null && typeof data === 'object' && Object.keys(data).length === 0) {
1581
+ return args[1]
1582
+ }
1583
+ if (Array.isArray(data) && data.length === 0) {
1584
+ return args[1]
1585
+ }
1586
+ if (typeof data === 'string' && data === '') {
1587
+ return args[1]
1588
+ }
1589
+ if (data === null || data === undefined) {
1590
+ return args[1]
1591
+ }
1592
+ return args[0]
1593
+ }
1594
+
1595
+
1596
+ ;// ./lib/models/templateCompiler/helpers/_join.js
1288
1597
  function _join(data, delimiter) {
1289
1598
  try {
1290
- return data.join(delimiter)
1599
+ if (data.length === 0) return ''
1600
+ if (data.length === 1) return _stringifyObject(data[0])
1601
+ return data.map((item) => _stringifyObject(item)).join(delimiter)
1291
1602
  } catch (e) {
1292
1603
  throw e
1293
1604
  }
1294
1605
  }
1295
1606
 
1607
+ function _stringifyObject(obj) {
1608
+ return JSON.stringify(obj).replace(/"([^"]+)":/g, '$1: ').replace(/"([^"]+)"/g, '$1').replace(/,/g, ', ')
1609
+ }
1610
+
1611
+
1612
+
1613
+ ;// ./lib/models/templateCompiler/helpers/_lt.js
1614
+
1615
+
1616
+
1617
+
1618
+ function _lt(data, args) {
1619
+ if (args.length !== 3) {
1620
+ throw new TemplateCompilerException(`_lt: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1621
+ }
1622
+ if (data === null || (typeof data === 'undefined')) {
1623
+ return null
1624
+ }
1625
+ if (args.includes(_SELF)) {
1626
+ args = args.map((arg) => {
1627
+ return (arg === _SELF) ? data : arg
1628
+ })
1629
+ }
1630
+ const expected = args[0]
1631
+ return data < expected ? args[1] : args[2]
1632
+ }
1633
+
1296
1634
 
1297
1635
 
1298
- ;// ./lib/models/template/helpers/_map.js
1636
+ ;// ./lib/models/templateCompiler/helpers/_lte.js
1637
+
1638
+
1639
+
1640
+
1641
+ function _lte(data, args) {
1642
+ if (args.length !== 3) {
1643
+ throw new TemplateCompilerException(`_lte: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1644
+ }
1645
+ if (data === null || (typeof data === 'undefined')) {
1646
+ return null
1647
+ }
1648
+ if (args.includes(_SELF)) {
1649
+ args = args.map((arg) => {
1650
+ return (arg === _SELF) ? data : arg
1651
+ })
1652
+ }
1653
+ const expected = args[0]
1654
+ return data <= expected ? args[1] : args[2]
1655
+ }
1656
+
1657
+
1658
+
1659
+ ;// ./lib/models/templateCompiler/helpers/_map.js
1299
1660
 
1300
1661
 
1301
1662
 
1302
1663
  function _map(data, args) {
1303
1664
  try {
1304
1665
  if (args.length === 0) {
1305
- throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1666
+ throw new TemplateCompilerException(TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentEmptyException)
1306
1667
  }
1307
1668
  if (data === null || (typeof data === 'undefined')) {
1308
1669
  return null
@@ -1344,43 +1705,162 @@ function _map(data, args) {
1344
1705
 
1345
1706
 
1346
1707
 
1347
- ;// ./lib/models/template/helpers/index.js
1708
+ ;// ./lib/models/templateCompiler/helpers/_neq.js
1348
1709
 
1349
1710
 
1350
1711
 
1351
1712
 
1713
+ function _neq(data, args) {
1714
+ if (args.length !== 3) {
1715
+ throw new TemplateCompilerException(`_neq: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1716
+ }
1717
+ if (data === null || (typeof data === 'undefined')) {
1718
+ return null
1719
+ }
1720
+ if (args.includes(_SELF)) {
1721
+ args = args.map((arg) => {
1722
+ return (arg === _SELF) ? data : arg
1723
+ })
1724
+ }
1725
+ const expected = args[0]
1726
+ return data !== expected ? args[1] : args[2]
1727
+ }
1352
1728
 
1353
1729
 
1354
1730
 
1731
+ ;// ./lib/models/templateCompiler/helpers/_toLowerCase.js
1355
1732
 
1356
1733
 
1357
- ;// ./lib/models/template/template.js
1734
+ function _toLowerCase(data, args) {
1735
+ if (args !== undefined) {
1736
+ throw new TemplateCompilerException(`_toLowerCase: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1737
+ }
1738
+ if (data === null || (typeof data === 'undefined') || typeof data !== 'string') {
1739
+ return null
1740
+ }
1741
+ return String(data).toLowerCase()
1742
+ }
1743
+
1358
1744
 
1359
1745
 
1746
+ ;// ./lib/models/templateCompiler/helpers/_toUpperCase.js
1360
1747
 
1361
1748
 
1362
- class Template {
1363
- constructor({ data }) {
1749
+ function _toUpperCase(data, args) {
1750
+ if (typeof data !== 'string') {
1751
+ throw new TemplateCompilerException(`_toUpperCase: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: the data must be string: ${data}`)
1752
+ }
1753
+ if (args !== undefined) {
1754
+ throw new TemplateCompilerException(`_toUpperCase: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: the argument must be empty: ${args.join(', ')}`)
1755
+ }
1756
+ if (data === null || (typeof data === 'undefined') || typeof data !== 'string') {
1757
+ return null
1758
+ }
1759
+ return String(data).toUpperCase()
1760
+ }
1761
+
1762
+
1763
+
1764
+ ;// ./lib/models/templateCompiler/helpers/index.js
1765
+
1766
+
1767
+
1768
+
1769
+
1770
+
1771
+
1772
+
1773
+
1774
+
1775
+
1776
+
1777
+
1778
+
1779
+
1780
+
1781
+
1782
+
1783
+
1784
+
1785
+
1786
+ ;// ./lib/models/templateCompiler/templateCompiler.js
1787
+
1788
+
1789
+
1790
+
1791
+ class TemplateCompiler {
1792
+ constructor(data) {
1364
1793
  this.data = data
1365
1794
  }
1795
+ static init(options) {
1796
+ return new this(options)
1797
+ }
1798
+ static initFromArray(arr = []) {
1799
+ if (Array.isArray(arr)) {
1800
+ return arr.map((a) => this.init(a))
1801
+ }
1802
+ return []
1803
+ }
1804
+ static initOnlyValidFromArray(arr = []) {
1805
+ return this.initFromArray(arr).filter((i) => i)
1806
+ }
1807
+ static concatIf(data, args) {
1808
+ return _concatIf(data, args)
1809
+ }
1366
1810
  static eq(data, args) {
1367
1811
  return _eq(data, args)
1368
1812
  }
1813
+
1814
+ static filterAll(data, args) {
1815
+ return _filterAll(data, args)
1816
+ }
1817
+
1818
+ static formatDate(data, args) {
1819
+ return _formatDate(data, args)
1820
+ }
1821
+
1369
1822
  static get(data, key, failover = null) {
1370
1823
  return _get(data, key, failover)
1371
1824
  }
1825
+ static gt(data, args) {
1826
+ return _gt(data, args)
1827
+ }
1828
+ static gte(data, args) {
1829
+ return _gte(data, args)
1830
+ }
1831
+ static isEmpty(data, args) {
1832
+ return _isEmpty(data, args)
1833
+ }
1834
+ static isNotEmpty(data, args) {
1835
+ return _isNotEmpty(data, args)
1836
+ }
1372
1837
  static join(data, separator = '') {
1373
1838
  return _join(data, separator)
1374
1839
  }
1840
+ static lt(data, args) {
1841
+ return _lt(data, args)
1842
+ }
1843
+ static lte(data, args) {
1844
+ return _lte(data, args)
1845
+ }
1375
1846
  static map(data, args = []) {
1376
1847
  return _map(data, args)
1377
1848
  }
1849
+ static neq(data, args) {
1850
+ return _neq(data, args)
1851
+ }
1852
+ static toLowerCase(data, args) {
1853
+ return _toLowerCase(data, args)
1854
+ }
1855
+ static toUpperCase(data, args) {
1856
+ return _toUpperCase(data, args)
1857
+ }
1378
1858
  static parseFunction(expression) {
1379
1859
  return _parseFunction(expression, _FN_NAMES)
1380
1860
  }
1381
1861
 
1382
1862
  pipe(expression = '') {
1383
- this.delimiters = expression.substring(0,2) === '<%' ? TAGS_EJS : TAGS_HANDLEBAR
1863
+ this.delimiters = expression.substring(0, 2) === '{{' ? TAGS_HANDLEBAR : TAGS_EJS
1384
1864
  const regex = new RegExp(`${this.delimiters[0]}\\s(.*?)\\s${this.delimiters[1]}`)
1385
1865
  const match = expression.match(regex)
1386
1866
  if (match !== null) {
@@ -1390,10 +1870,10 @@ class Template {
1390
1870
  return _callFunction(acc, fn.name, fn.args)
1391
1871
  }, this.data)
1392
1872
  } catch (e) {
1393
- throw new TemplateException(`Template engine error: ${e.message}`)
1873
+ throw new TemplateCompilerException(`TemplateCompiler engine error: ${e.message}`)
1394
1874
  }
1395
1875
  }
1396
- throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.invalidRegExpException)
1876
+ throw new TemplateCompilerException(`TemplateCompiler engine error: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.invalidRegExpException}`)
1397
1877
  }
1398
1878
  }
1399
1879
 
@@ -1417,7 +1897,7 @@ function _parseFunction(expression, existFunctionNames) {
1417
1897
  args: paramList
1418
1898
  })
1419
1899
  } else {
1420
- throw new TemplateException(`${functionName} is not a valid function`)
1900
+ throw new TemplateCompilerException(`${functionName} is not a valid function`)
1421
1901
  }
1422
1902
  }
1423
1903
  return acc
@@ -1461,12 +1941,18 @@ function _parseSinglePart(input) {
1461
1941
  return input.substring(1, input.length - 1)
1462
1942
  }
1463
1943
 
1944
+ const _input = _toBasicType(input)
1945
+
1946
+ if (typeof _input !== 'string') {
1947
+ return _input
1948
+ }
1949
+
1464
1950
  // 如果是一个列表形式(例如 ["p", "d"] 或 [p, d])
1465
- if (input.startsWith('[') && input.endsWith(']')) {
1466
- const listContent = input.substring(1, input.length - 1).trim()
1951
+ if (_input.startsWith('[') && _input.endsWith(']')) {
1952
+ const listContent = _input.substring(1, _input.length - 1).trim()
1467
1953
  if (listContent !== '') {
1468
1954
  return listContent.split(',').map((item) => {
1469
- return item.trim().replaceAll('"', '') // 去除可能的双引号
1955
+ return _toBasicType(item.trim())
1470
1956
  })
1471
1957
  }
1472
1958
  return []
@@ -1476,59 +1962,75 @@ function _parseSinglePart(input) {
1476
1962
  return input
1477
1963
  }
1478
1964
 
1965
+ function _toBasicType(input) {
1966
+ if (input.startsWith('"') && input.endsWith('"')) {
1967
+ // 去掉双引号,返回
1968
+ return input.substring(1, input.length - 1)
1969
+ }
1970
+ if (input === 'true') {
1971
+ return true
1972
+ }
1973
+ if (input === 'false') {
1974
+ return false
1975
+ }
1976
+ if (input === 'undefined') {
1977
+ return undefined
1978
+ }
1979
+ if (input === 'null') {
1980
+ return null
1981
+ }
1982
+ if (!Number.isNaN(input) && !Number.isNaN(Number.parseFloat(input))) {
1983
+ return Number(input)
1984
+ }
1985
+ return input
1986
+ }
1987
+
1479
1988
  function _callFunction(data, functionName, parameters) {
1480
1989
  try {
1481
1990
  let failover
1482
1991
  switch (functionName) {
1992
+ case 'exec':
1993
+ return _exec(data, parameters)
1483
1994
  case 'get':
1484
1995
  if (parameters.length > 2) {
1485
- throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentFormatException)
1996
+ throw new TemplateCompilerException(TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException)
1486
1997
  }
1487
1998
  if (parameters.length === 2) {
1488
1999
  failover = parameters[parameters.length - 1]
1489
2000
  }
1490
2001
  return _get(data, parameters[0], failover)
1491
- case 'map':
1492
- return _map(data, parameters)
1493
2002
  case 'join':
1494
2003
  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);
2004
+ case 'map':
2005
+ return _map(data, parameters)
2006
+ case 'concatIf':
2007
+ return _concatIf(data, parameters)
1503
2008
  case 'filterOne':
1504
2009
  return _filterOne(data, parameters)
1505
2010
  case 'filterAll':
1506
2011
  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);
2012
+ case 'formatDate':
2013
+ return _formatDate(data, parameters)
1512
2014
  case 'eq':
1513
2015
  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);
2016
+ case 'neq':
2017
+ return _neq(data, parameters)
2018
+ case 'gt':
2019
+ return _gt(data, parameters)
2020
+ case 'gte':
2021
+ return _gte(data, parameters)
2022
+ case 'lt':
2023
+ return _lt(data, parameters)
2024
+ case 'lte':
2025
+ return _lte(data, parameters)
2026
+ case 'isEmpty':
2027
+ return _isEmpty(data, parameters)
2028
+ case 'isNotEmpty':
2029
+ return _isNotEmpty(data, parameters)
2030
+ case 'toLowerCase':
2031
+ return _toLowerCase(data)
2032
+ case 'toUpperCase':
2033
+ return _toUpperCase(data)
1532
2034
  default:
1533
2035
  throw new Error(`${functionName} is not a valid function`)
1534
2036
  }
@@ -1539,7 +2041,7 @@ function _callFunction(data, functionName, parameters) {
1539
2041
 
1540
2042
 
1541
2043
 
1542
- ;// ./lib/models/template/index.js
2044
+ ;// ./lib/models/templateCompiler/index.js
1543
2045
 
1544
2046
 
1545
2047
 
@@ -1801,7 +2303,7 @@ var __webpack_exports__Metadata = __webpack_exports__.OS;
1801
2303
  var __webpack_exports__QMeta = __webpack_exports__.Z8;
1802
2304
  var __webpack_exports__Repo = __webpack_exports__.lc;
1803
2305
  var __webpack_exports__Service = __webpack_exports__.kl;
1804
- var __webpack_exports__Template = __webpack_exports__.Bj;
2306
+ var __webpack_exports__TemplateCompiler = __webpack_exports__.Mg;
1805
2307
  var __webpack_exports__UniqueKeyGenerator = __webpack_exports__._x;
1806
2308
  var __webpack_exports__convertString = __webpack_exports__.l0;
1807
2309
  var __webpack_exports__formatDate = __webpack_exports__.Yq;
@@ -1815,4 +2317,4 @@ var __webpack_exports__pReduce = __webpack_exports__.d;
1815
2317
  var __webpack_exports__padZeros = __webpack_exports__.Lv;
1816
2318
  var __webpack_exports__stringFormatter = __webpack_exports__.Qy;
1817
2319
  var __webpack_exports__stringHelper = __webpack_exports__.yO;
1818
- export { __webpack_exports__ApiResponse as ApiResponse, __webpack_exports__KeyValueObject as KeyValueObject, __webpack_exports__Metadata as Metadata, __webpack_exports__QMeta as QMeta, __webpack_exports__Repo as Repo, __webpack_exports__Service as Service, __webpack_exports__Template as Template, __webpack_exports__UniqueKeyGenerator as UniqueKeyGenerator, __webpack_exports__convertString as convertString, __webpack_exports__formatDate as formatDate, __webpack_exports__generalPost as generalPost, __webpack_exports__getValidation as getValidation, __webpack_exports__getValueByKeys as getValueByKeys, __webpack_exports__makeApiResponse as makeApiResponse, __webpack_exports__makeService as makeService, __webpack_exports__objectHelper as objectHelper, __webpack_exports__pReduce as pReduce, __webpack_exports__padZeros as padZeros, __webpack_exports__stringFormatter as stringFormatter, __webpack_exports__stringHelper as stringHelper };
2320
+ export { __webpack_exports__ApiResponse as ApiResponse, __webpack_exports__KeyValueObject as KeyValueObject, __webpack_exports__Metadata as Metadata, __webpack_exports__QMeta as QMeta, __webpack_exports__Repo as Repo, __webpack_exports__Service as Service, __webpack_exports__TemplateCompiler as TemplateCompiler, __webpack_exports__UniqueKeyGenerator as UniqueKeyGenerator, __webpack_exports__convertString as convertString, __webpack_exports__formatDate as formatDate, __webpack_exports__generalPost as generalPost, __webpack_exports__getValidation as getValidation, __webpack_exports__getValueByKeys as getValueByKeys, __webpack_exports__makeApiResponse as makeApiResponse, __webpack_exports__makeService as makeService, __webpack_exports__objectHelper as objectHelper, __webpack_exports__pReduce as pReduce, __webpack_exports__padZeros as padZeros, __webpack_exports__stringFormatter as stringFormatter, __webpack_exports__stringHelper as stringHelper };