@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.
@@ -55,7 +55,7 @@ __webpack_require__.d(__webpack_exports__, {
55
55
  QMeta: () => (/* reexport */ QMeta),
56
56
  Repo: () => (/* reexport */ Repo),
57
57
  Service: () => (/* reexport */ Service),
58
- Template: () => (/* reexport */ Template),
58
+ TemplateCompiler: () => (/* reexport */ TemplateCompiler),
59
59
  UniqueKeyGenerator: () => (/* reexport */ UniqueKeyGenerator),
60
60
  convertString: () => (/* reexport */ convertString),
61
61
  formatDate: () => (/* reexport */ formatDate),
@@ -232,6 +232,16 @@ function getValidation(rule, data, getDataByKey, KeyValueObject) {
232
232
  }
233
233
  return false
234
234
  }
235
+ case '$intervalTimeGt': {
236
+ const now = new Date().getTime()
237
+ const timestamp = new Date(rowValue).getTime()
238
+ return (now - timestamp) > value['$intervalTimeGt']
239
+ }
240
+ case '$intervalTimeLt': {
241
+ const now = new Date().getTime()
242
+ const timestamp = new Date(rowValue).getTime()
243
+ return (now - timestamp) < value['$intervalTimeLt']
244
+ }
235
245
  case '$notInValue': {
236
246
  const result = getDataByKey(value['$notInValue'], data)
237
247
  const _value = Array.isArray(result) ? result : []
@@ -1146,8 +1156,8 @@ function makeService({ repo }) {
1146
1156
 
1147
1157
 
1148
1158
 
1149
- ;// ./lib/models/template/templateException.js
1150
- const TEMPLATE_EXCEPTION_TYPE = {
1159
+ ;// ./lib/models/templateCompiler/templateCompilerException.js
1160
+ const TEMPLATE_COMPILER_EXCEPTION_TYPE = {
1151
1161
  argumentEmptyException: 'Argument is empty',
1152
1162
  argumentFormatException: 'Incorrect number or format of argument',
1153
1163
  invalidFuntionException: 'Function Name is invalid',
@@ -1158,7 +1168,7 @@ const TEMPLATE_EXCEPTION_TYPE = {
1158
1168
  resultMoreThanOneException: 'More than one result'
1159
1169
  }
1160
1170
 
1161
- class TemplateException extends Error {
1171
+ class TemplateCompilerException extends Error {
1162
1172
  constructor(message) {
1163
1173
  super(message)
1164
1174
  this.message = message
@@ -1167,10 +1177,10 @@ class TemplateException extends Error {
1167
1177
 
1168
1178
 
1169
1179
 
1170
- ;// ./lib/models/template/constants.js
1180
+ ;// ./lib/models/templateCompiler/constants.js
1171
1181
  const _EMPTY = '_EMPTY'
1172
1182
  const _FN_NAMES = [
1173
- 'get', 'map', 'join', 'concatIf', 'filterOne', 'filterAll', 'formatDate', 'eq', 'neq', 'gt', 'lt', 'gte', 'lte', 'isEmpty', 'isNotEmpty', 'toLowerCase', 'toUpperCase'
1183
+ 'get', 'map', 'join', 'concatIf', 'exec', 'filterOne', 'filterAll', 'formatDate', 'eq', 'neq', 'gt', 'lt', 'gte', 'lte', 'isEmpty', 'isNotEmpty', 'toLowerCase', 'toUpperCase'
1174
1184
  ]
1175
1185
  const _HIDE = '_HIDE'
1176
1186
  const _NOT_EMPTY = '_NOT_EMPTY'
@@ -1180,14 +1190,45 @@ const TAGS_HANDLEBAR = ['{{', '}}']
1180
1190
 
1181
1191
 
1182
1192
 
1183
- ;// ./lib/models/template/helpers/_eq.js
1193
+ ;// ./lib/models/templateCompiler/helpers/_concatIf.js
1194
+
1195
+
1196
+
1197
+
1198
+ function _concatIf(data, args) {
1199
+ if (typeof data !== 'string') {
1200
+ throw new TemplateCompilerException(`_concatIf: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: the data must be string :${data.join(', ')}`)
1201
+ }
1202
+ if (args.length !== 3) {
1203
+ throw new TemplateCompilerException(`_concatIf: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1204
+ }
1205
+ if (data === null || (typeof data === 'undefined')) {
1206
+ return null
1207
+ }
1208
+ const [condition, success, failover] = args
1209
+ const validConditions = [_EMPTY, _NOT_EMPTY]
1210
+ if (validConditions.includes(condition) || success.length !== 2) {
1211
+ throw new TemplateCompilerException(`concatIf: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentEmptyException}: ${condition}, ${success}`)
1212
+ }
1213
+ if (data === '' && failover.includes(_HIDE)) {
1214
+ return ''
1215
+ }
1216
+ if (data !== '' && (data !== null || data !== undefined) && failover.includes(_HIDE)) {
1217
+ return `${success[0]}${data}${success[success.length - 1]}`
1218
+ }
1219
+ return failover
1220
+ }
1221
+
1222
+
1223
+
1224
+ ;// ./lib/models/templateCompiler/helpers/_eq.js
1184
1225
 
1185
1226
 
1186
1227
 
1187
1228
 
1188
1229
  function _eq(data, args) {
1189
1230
  if (args.length !== 3) {
1190
- throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentFormatException)
1231
+ throw new TemplateCompilerException(`eq: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1191
1232
  }
1192
1233
  if (data === null || (typeof data === 'undefined')) {
1193
1234
  return null
@@ -1203,15 +1244,28 @@ function _eq(data, args) {
1203
1244
 
1204
1245
 
1205
1246
 
1206
- ;// ./lib/models/template/helpers/_filterAll.js
1247
+ ;// ./lib/models/templateCompiler/helpers/_exec.js
1248
+ function _exec(data, args) {
1249
+ try {
1250
+ const [methodName, ..._args] = args
1251
+ return data[methodName](..._args)
1252
+ } catch (e) {
1253
+ throw e
1254
+ }
1255
+ }
1207
1256
 
1208
1257
 
1209
- const _filterAll_DELIMITER = '~~~'
1258
+
1259
+ ;// ./lib/models/templateCompiler/helpers/_filterAll.js
1260
+
1261
+
1262
+
1263
+ // const DELIMITER = '~~~'
1210
1264
 
1211
1265
  function _filterAll(data, args) {
1212
1266
  try {
1213
1267
  if (!Array.isArray(args) || args.length === 0) {
1214
- throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1268
+ throw new TemplateCompilerException(TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentEmptyException)
1215
1269
  }
1216
1270
  if (!Array.isArray(data) || data.length === 0) {
1217
1271
  return []
@@ -1229,25 +1283,140 @@ function _filterAll(data, args) {
1229
1283
  }
1230
1284
 
1231
1285
  function _exist(data, args) {
1232
- const _str = `${_filterAll_DELIMITER}${args.join(_filterAll_DELIMITER)}${_filterAll_DELIMITER}`
1233
- return data.filter((e) => {
1234
- return _str.includes(`${_filterAll_DELIMITER}${e}${_filterAll_DELIMITER}`) && args.includes(e)
1235
- })
1286
+ const _args = args.flat()
1287
+ return data.filter((e) => _args.some((arg) => _performOperation(arg, e)))
1236
1288
  }
1237
1289
 
1238
1290
  function _existObject(data, args) {
1239
- if (args.length !== 2) {
1240
- return []
1291
+ if (args.length === 1) {
1292
+ const arg = args[0]
1293
+ return data.filter((e) => {
1294
+ if (arg.includes('.')) {
1295
+ return getValueByKeys(arg.split('.'), e)
1296
+ }
1297
+ return Object.prototype.hasOwnProperty.call(e, arg)
1298
+ })
1241
1299
  }
1242
- const [key, _args] = args
1300
+
1301
+ if (args.length > 2) {
1302
+ let res = data
1303
+ for (let i = 0; i < args.length; i += 2) {
1304
+ const group = [args[i], args[i + 1]]
1305
+ res = _existObject(res, group)
1306
+ }
1307
+ return res
1308
+ }
1309
+
1310
+ const [key, ..._argsArr] = args
1311
+ const _args = _argsArr.flat()
1243
1312
  return data.filter((e) => {
1244
- return _args.includes(e[key])
1313
+ const value = key.includes('.') ? getValueByKeys(key.split('.'), e) : e[key]
1314
+ return _args.some((arg) => _performOperation(arg, value))
1245
1315
  })
1246
1316
  }
1247
1317
 
1318
+ function _performOperation(arg, value) {
1319
+ // the arg is undefined
1320
+ if (arg === undefined && value === undefined) return true
1248
1321
 
1322
+ // the arg is null
1323
+ if (arg === null && value === null) return true
1249
1324
 
1250
- ;// ./lib/models/template/helpers/_filterOne.js
1325
+ // the arg is boolean
1326
+ if (typeof arg === 'boolean') {
1327
+ return arg === value
1328
+ }
1329
+
1330
+ // the arg is blank or *: Blank => Empty, * => Not Empty
1331
+ if (arg === '' || arg === '*') {
1332
+ // null and undefined are not included in either case
1333
+ if (value === null || value === undefined) {
1334
+ return false
1335
+ }
1336
+ if (typeof value === 'string') {
1337
+ return arg === '' ? value === '' : value !== ''
1338
+ }
1339
+ if (Array.isArray(value)) {
1340
+ return arg === '' ? value.length === 0 : value.length !== 0
1341
+ }
1342
+ return arg !== ''
1343
+ }
1344
+
1345
+ // the arg is alphabetic or number
1346
+ if (_isPureStringOrNumber(arg)) {
1347
+ return arg === value
1348
+ }
1349
+
1350
+ // the arg is array of [] or [*]: [] => Empty, [*] => Not Empty
1351
+ if (arg.startsWith('[') && arg.endsWith(']')) {
1352
+ if (arg === '[]') {
1353
+ return Array.isArray(value) && value.length === 0
1354
+ }
1355
+ if (arg === '[*]') {
1356
+ return Array.isArray(value) && value.length !== 0
1357
+ }
1358
+ return false
1359
+ }
1360
+
1361
+ // the arg is 'operator + string | number'
1362
+ const { operator, value: argValue } = _splitOperator(arg)
1363
+ if (!operator || (argValue !== 0 && !argValue)) {
1364
+ return false
1365
+ }
1366
+ switch (operator) {
1367
+ case '>':
1368
+ return value > argValue
1369
+ case '<':
1370
+ return value < argValue
1371
+ case '!=':
1372
+ return value !== argValue
1373
+ case '>=':
1374
+ return value >= argValue
1375
+ case '<=':
1376
+ return value <= argValue
1377
+ default:
1378
+ return false
1379
+ }
1380
+ }
1381
+
1382
+ function _isPureStringOrNumber(input) {
1383
+ if (typeof input === 'string') {
1384
+ if (input.startsWith('[') && input.endsWith(']')) {
1385
+ return false
1386
+ }
1387
+ if (/!=|>=|<=|>|</.test(input)) {
1388
+ return false
1389
+ }
1390
+ return true
1391
+ }
1392
+ return !Number.isNaN(input)
1393
+ }
1394
+
1395
+ function _splitOperator(str) {
1396
+ const operators = ['!=', '>=', '<=', '>', '<']
1397
+
1398
+ const matchedOp = operators.find((op) => str.startsWith(op))
1399
+ if (!matchedOp) return { operator: null, value: null }
1400
+
1401
+ const remaining = str.slice(matchedOp.length)
1402
+
1403
+ // '>Primary' or '<Primary' is invalid
1404
+ if (/^[a-zA-Z]*$/.test(remaining) && matchedOp !== '!=') {
1405
+ return { operator: null, value: null }
1406
+ }
1407
+
1408
+ // if it is a number it is converted to a number
1409
+ const value = (!Number.isNaN(parseFloat(remaining)) && !Number.isNaN(remaining)) ? Number(remaining) : remaining
1410
+
1411
+ return {
1412
+ operator: matchedOp,
1413
+ value
1414
+ }
1415
+ }
1416
+
1417
+
1418
+
1419
+ ;// ./lib/models/templateCompiler/helpers/_filterOne.js
1251
1420
 
1252
1421
 
1253
1422
 
@@ -1260,7 +1429,7 @@ function _filterOne(data, args) {
1260
1429
  if (list.length === 0) {
1261
1430
  return null
1262
1431
  }
1263
- throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.resultMoreThanOneException)
1432
+ throw new TemplateCompilerException(TEMPLATE_COMPILER_EXCEPTION_TYPE.resultMoreThanOneException)
1264
1433
  } catch (e) {
1265
1434
  throw e
1266
1435
  }
@@ -1268,13 +1437,49 @@ function _filterOne(data, args) {
1268
1437
 
1269
1438
 
1270
1439
 
1271
- ;// ./lib/models/template/helpers/_get.js
1440
+ ;// ./lib/models/templateCompiler/helpers/_formatDate.js
1441
+
1442
+
1443
+ function _formatDate(timestamp, format) {
1444
+ if (format.length === 0) {
1445
+ throw new TemplateCompilerException(`_formateDate: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: format parts must be not empty array`)
1446
+ }
1447
+
1448
+ if (timestamp === null || timestamp === undefined) {
1449
+ return null
1450
+ }
1451
+
1452
+ const date = new Date(timestamp)
1453
+
1454
+ const partsMap = {
1455
+ yyyy: String(date.getFullYear()),
1456
+ mm: String(date.getMonth() + 1).padStart(2, '0'),
1457
+ dd: String(date.getDate()).padStart(2, '0')
1458
+ }
1459
+
1460
+ // Check for invalid format tokens
1461
+ const validTokens = ['yyyy', 'mm', 'dd']
1462
+ const invalidTokens = format.filter((part) => part.length > 1 && !validTokens.includes(part))
1463
+
1464
+ if (invalidTokens.length > 0) {
1465
+ throw new TemplateCompilerException(
1466
+ `_formateDate: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: the format type is not valid: ${format.join(', ')}`
1467
+ )
1468
+ }
1469
+
1470
+ // Build the formatted string using reduce
1471
+ return format.reduce((result, part) => result + (partsMap[part] || part), '')
1472
+ }
1473
+
1474
+
1475
+
1476
+ ;// ./lib/models/templateCompiler/helpers/_get.js
1272
1477
 
1273
1478
 
1274
1479
  function _get(data, key, failover = null) {
1275
1480
  try {
1276
1481
  if (key === null || (typeof key === 'undefined') || key === '') {
1277
- throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1482
+ throw new TemplateCompilerException(TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentEmptyException)
1278
1483
  }
1279
1484
  if (data === null) {
1280
1485
  return null
@@ -1304,30 +1509,186 @@ function _handleFailover(key, failover) {
1304
1509
  return failover
1305
1510
  }
1306
1511
  return null
1307
- // throw new TemplateException(`Key "${key}" does not exist and no failover`)
1512
+ // throw new TemplateCompilerException(`Key "${key}" does not exist and no failover`)
1513
+ }
1514
+
1515
+
1516
+
1517
+ ;// ./lib/models/templateCompiler/helpers/_gt.js
1518
+
1519
+
1520
+
1521
+
1522
+ function _gt(data, args) {
1523
+ if (args.length !== 3) {
1524
+ throw new TemplateCompilerException(`_gt: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1525
+ }
1526
+ if (data === null || (typeof data === 'undefined')) {
1527
+ return null
1528
+ }
1529
+ if (args.includes(_SELF)) {
1530
+ args = args.map((arg) => {
1531
+ return (arg === _SELF) ? data : arg
1532
+ })
1533
+ }
1534
+ const expected = args[0]
1535
+ return data > expected ? args[1] : args[2]
1536
+ }
1537
+
1538
+
1539
+
1540
+ ;// ./lib/models/templateCompiler/helpers/_gte.js
1541
+
1542
+
1543
+
1544
+
1545
+ function _gte(data, args) {
1546
+ if (args.length !== 3) {
1547
+ throw new TemplateCompilerException(`_gte: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1548
+ }
1549
+ if (data === null || (typeof data === 'undefined')) {
1550
+ return null
1551
+ }
1552
+ if (args.includes(_SELF)) {
1553
+ args = args.map((arg) => {
1554
+ return (arg === _SELF) ? data : arg
1555
+ })
1556
+ }
1557
+ const expected = args[0]
1558
+ return data >= expected ? args[1] : args[2]
1559
+ }
1560
+
1561
+
1562
+
1563
+ ;// ./lib/models/templateCompiler/helpers/_isEmpty.js
1564
+
1565
+
1566
+
1567
+
1568
+ function _isEmpty(data, args) {
1569
+ if (args.length !== 2) {
1570
+ throw new TemplateCompilerException(`_isEmpty: ${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[0]
1582
+ }
1583
+ return (data === '' || data === null || data === undefined || data.length === 0) ? args[0] : args[1]
1308
1584
  }
1309
1585
 
1310
1586
 
1311
1587
 
1312
- ;// ./lib/models/template/helpers/_join.js
1588
+ ;// ./lib/models/templateCompiler/helpers/_isNotEmpty.js
1589
+
1590
+
1591
+
1592
+
1593
+ function _isNotEmpty(data, args) {
1594
+ if (args.length !== 2) {
1595
+ throw new TemplateCompilerException(`_isNotEmpty: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1596
+ }
1597
+ // if (data === null || (typeof data === 'undefined')) {
1598
+ // return null
1599
+ // }
1600
+ if (args.includes(_SELF)) {
1601
+ args = args.map((arg) => {
1602
+ return (arg === _SELF) ? data : arg
1603
+ })
1604
+ }
1605
+ if (data !== null && typeof data === 'object' && Object.keys(data).length === 0) {
1606
+ return args[1]
1607
+ }
1608
+ if (Array.isArray(data) && data.length === 0) {
1609
+ return args[1]
1610
+ }
1611
+ if (typeof data === 'string' && data === '') {
1612
+ return args[1]
1613
+ }
1614
+ if (data === null || data === undefined) {
1615
+ return args[1]
1616
+ }
1617
+ return args[0]
1618
+ }
1619
+
1620
+
1621
+ ;// ./lib/models/templateCompiler/helpers/_join.js
1313
1622
  function _join(data, delimiter) {
1314
1623
  try {
1315
- return data.join(delimiter)
1624
+ if (data.length === 0) return ''
1625
+ if (data.length === 1) return _stringifyObject(data[0])
1626
+ return data.map((item) => _stringifyObject(item)).join(delimiter)
1316
1627
  } catch (e) {
1317
1628
  throw e
1318
1629
  }
1319
1630
  }
1320
1631
 
1632
+ function _stringifyObject(obj) {
1633
+ return JSON.stringify(obj).replace(/"([^"]+)":/g, '$1: ').replace(/"([^"]+)"/g, '$1').replace(/,/g, ', ')
1634
+ }
1635
+
1636
+
1637
+
1638
+ ;// ./lib/models/templateCompiler/helpers/_lt.js
1639
+
1640
+
1641
+
1642
+
1643
+ function _lt(data, args) {
1644
+ if (args.length !== 3) {
1645
+ throw new TemplateCompilerException(`_lt: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1646
+ }
1647
+ if (data === null || (typeof data === 'undefined')) {
1648
+ return null
1649
+ }
1650
+ if (args.includes(_SELF)) {
1651
+ args = args.map((arg) => {
1652
+ return (arg === _SELF) ? data : arg
1653
+ })
1654
+ }
1655
+ const expected = args[0]
1656
+ return data < expected ? args[1] : args[2]
1657
+ }
1658
+
1321
1659
 
1322
1660
 
1323
- ;// ./lib/models/template/helpers/_map.js
1661
+ ;// ./lib/models/templateCompiler/helpers/_lte.js
1662
+
1663
+
1664
+
1665
+
1666
+ function _lte(data, args) {
1667
+ if (args.length !== 3) {
1668
+ throw new TemplateCompilerException(`_lte: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1669
+ }
1670
+ if (data === null || (typeof data === 'undefined')) {
1671
+ return null
1672
+ }
1673
+ if (args.includes(_SELF)) {
1674
+ args = args.map((arg) => {
1675
+ return (arg === _SELF) ? data : arg
1676
+ })
1677
+ }
1678
+ const expected = args[0]
1679
+ return data <= expected ? args[1] : args[2]
1680
+ }
1681
+
1682
+
1683
+
1684
+ ;// ./lib/models/templateCompiler/helpers/_map.js
1324
1685
 
1325
1686
 
1326
1687
 
1327
1688
  function _map(data, args) {
1328
1689
  try {
1329
1690
  if (args.length === 0) {
1330
- throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentEmptyException)
1691
+ throw new TemplateCompilerException(TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentEmptyException)
1331
1692
  }
1332
1693
  if (data === null || (typeof data === 'undefined')) {
1333
1694
  return null
@@ -1369,43 +1730,162 @@ function _map(data, args) {
1369
1730
 
1370
1731
 
1371
1732
 
1372
- ;// ./lib/models/template/helpers/index.js
1733
+ ;// ./lib/models/templateCompiler/helpers/_neq.js
1373
1734
 
1374
1735
 
1375
1736
 
1376
1737
 
1738
+ function _neq(data, args) {
1739
+ if (args.length !== 3) {
1740
+ throw new TemplateCompilerException(`_neq: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1741
+ }
1742
+ if (data === null || (typeof data === 'undefined')) {
1743
+ return null
1744
+ }
1745
+ if (args.includes(_SELF)) {
1746
+ args = args.map((arg) => {
1747
+ return (arg === _SELF) ? data : arg
1748
+ })
1749
+ }
1750
+ const expected = args[0]
1751
+ return data !== expected ? args[1] : args[2]
1752
+ }
1377
1753
 
1378
1754
 
1379
1755
 
1756
+ ;// ./lib/models/templateCompiler/helpers/_toLowerCase.js
1380
1757
 
1381
1758
 
1382
- ;// ./lib/models/template/template.js
1759
+ function _toLowerCase(data, args) {
1760
+ if (args !== undefined) {
1761
+ throw new TemplateCompilerException(`_toLowerCase: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: ${args.join(', ')}`)
1762
+ }
1763
+ if (data === null || (typeof data === 'undefined') || typeof data !== 'string') {
1764
+ return null
1765
+ }
1766
+ return String(data).toLowerCase()
1767
+ }
1768
+
1383
1769
 
1384
1770
 
1771
+ ;// ./lib/models/templateCompiler/helpers/_toUpperCase.js
1385
1772
 
1386
1773
 
1387
- class Template {
1388
- constructor({ data }) {
1774
+ function _toUpperCase(data, args) {
1775
+ if (typeof data !== 'string') {
1776
+ throw new TemplateCompilerException(`_toUpperCase: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: the data must be string: ${data}`)
1777
+ }
1778
+ if (args !== undefined) {
1779
+ throw new TemplateCompilerException(`_toUpperCase: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException}: the argument must be empty: ${args.join(', ')}`)
1780
+ }
1781
+ if (data === null || (typeof data === 'undefined') || typeof data !== 'string') {
1782
+ return null
1783
+ }
1784
+ return String(data).toUpperCase()
1785
+ }
1786
+
1787
+
1788
+
1789
+ ;// ./lib/models/templateCompiler/helpers/index.js
1790
+
1791
+
1792
+
1793
+
1794
+
1795
+
1796
+
1797
+
1798
+
1799
+
1800
+
1801
+
1802
+
1803
+
1804
+
1805
+
1806
+
1807
+
1808
+
1809
+
1810
+
1811
+ ;// ./lib/models/templateCompiler/templateCompiler.js
1812
+
1813
+
1814
+
1815
+
1816
+ class TemplateCompiler {
1817
+ constructor(data) {
1389
1818
  this.data = data
1390
1819
  }
1820
+ static init(options) {
1821
+ return new this(options)
1822
+ }
1823
+ static initFromArray(arr = []) {
1824
+ if (Array.isArray(arr)) {
1825
+ return arr.map((a) => this.init(a))
1826
+ }
1827
+ return []
1828
+ }
1829
+ static initOnlyValidFromArray(arr = []) {
1830
+ return this.initFromArray(arr).filter((i) => i)
1831
+ }
1832
+ static concatIf(data, args) {
1833
+ return _concatIf(data, args)
1834
+ }
1391
1835
  static eq(data, args) {
1392
1836
  return _eq(data, args)
1393
1837
  }
1838
+
1839
+ static filterAll(data, args) {
1840
+ return _filterAll(data, args)
1841
+ }
1842
+
1843
+ static formatDate(data, args) {
1844
+ return _formatDate(data, args)
1845
+ }
1846
+
1394
1847
  static get(data, key, failover = null) {
1395
1848
  return _get(data, key, failover)
1396
1849
  }
1850
+ static gt(data, args) {
1851
+ return _gt(data, args)
1852
+ }
1853
+ static gte(data, args) {
1854
+ return _gte(data, args)
1855
+ }
1856
+ static isEmpty(data, args) {
1857
+ return _isEmpty(data, args)
1858
+ }
1859
+ static isNotEmpty(data, args) {
1860
+ return _isNotEmpty(data, args)
1861
+ }
1397
1862
  static join(data, separator = '') {
1398
1863
  return _join(data, separator)
1399
1864
  }
1865
+ static lt(data, args) {
1866
+ return _lt(data, args)
1867
+ }
1868
+ static lte(data, args) {
1869
+ return _lte(data, args)
1870
+ }
1400
1871
  static map(data, args = []) {
1401
1872
  return _map(data, args)
1402
1873
  }
1874
+ static neq(data, args) {
1875
+ return _neq(data, args)
1876
+ }
1877
+ static toLowerCase(data, args) {
1878
+ return _toLowerCase(data, args)
1879
+ }
1880
+ static toUpperCase(data, args) {
1881
+ return _toUpperCase(data, args)
1882
+ }
1403
1883
  static parseFunction(expression) {
1404
1884
  return _parseFunction(expression, _FN_NAMES)
1405
1885
  }
1406
1886
 
1407
1887
  pipe(expression = '') {
1408
- this.delimiters = expression.substring(0,2) === '<%' ? TAGS_EJS : TAGS_HANDLEBAR
1888
+ this.delimiters = expression.substring(0, 2) === '{{' ? TAGS_HANDLEBAR : TAGS_EJS
1409
1889
  const regex = new RegExp(`${this.delimiters[0]}\\s(.*?)\\s${this.delimiters[1]}`)
1410
1890
  const match = expression.match(regex)
1411
1891
  if (match !== null) {
@@ -1415,10 +1895,10 @@ class Template {
1415
1895
  return _callFunction(acc, fn.name, fn.args)
1416
1896
  }, this.data)
1417
1897
  } catch (e) {
1418
- throw new TemplateException(`Template engine error: ${e.message}`)
1898
+ throw new TemplateCompilerException(`TemplateCompiler engine error: ${e.message}`)
1419
1899
  }
1420
1900
  }
1421
- throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.invalidRegExpException)
1901
+ throw new TemplateCompilerException(`TemplateCompiler engine error: ${TEMPLATE_COMPILER_EXCEPTION_TYPE.invalidRegExpException}`)
1422
1902
  }
1423
1903
  }
1424
1904
 
@@ -1442,7 +1922,7 @@ function _parseFunction(expression, existFunctionNames) {
1442
1922
  args: paramList
1443
1923
  })
1444
1924
  } else {
1445
- throw new TemplateException(`${functionName} is not a valid function`)
1925
+ throw new TemplateCompilerException(`${functionName} is not a valid function`)
1446
1926
  }
1447
1927
  }
1448
1928
  return acc
@@ -1486,12 +1966,18 @@ function _parseSinglePart(input) {
1486
1966
  return input.substring(1, input.length - 1)
1487
1967
  }
1488
1968
 
1969
+ const _input = _toBasicType(input)
1970
+
1971
+ if (typeof _input !== 'string') {
1972
+ return _input
1973
+ }
1974
+
1489
1975
  // 如果是一个列表形式(例如 ["p", "d"] 或 [p, d])
1490
- if (input.startsWith('[') && input.endsWith(']')) {
1491
- const listContent = input.substring(1, input.length - 1).trim()
1976
+ if (_input.startsWith('[') && _input.endsWith(']')) {
1977
+ const listContent = _input.substring(1, _input.length - 1).trim()
1492
1978
  if (listContent !== '') {
1493
1979
  return listContent.split(',').map((item) => {
1494
- return item.trim().replaceAll('"', '') // 去除可能的双引号
1980
+ return _toBasicType(item.trim())
1495
1981
  })
1496
1982
  }
1497
1983
  return []
@@ -1501,59 +1987,75 @@ function _parseSinglePart(input) {
1501
1987
  return input
1502
1988
  }
1503
1989
 
1990
+ function _toBasicType(input) {
1991
+ if (input.startsWith('"') && input.endsWith('"')) {
1992
+ // 去掉双引号,返回
1993
+ return input.substring(1, input.length - 1)
1994
+ }
1995
+ if (input === 'true') {
1996
+ return true
1997
+ }
1998
+ if (input === 'false') {
1999
+ return false
2000
+ }
2001
+ if (input === 'undefined') {
2002
+ return undefined
2003
+ }
2004
+ if (input === 'null') {
2005
+ return null
2006
+ }
2007
+ if (!Number.isNaN(input) && !Number.isNaN(Number.parseFloat(input))) {
2008
+ return Number(input)
2009
+ }
2010
+ return input
2011
+ }
2012
+
1504
2013
  function _callFunction(data, functionName, parameters) {
1505
2014
  try {
1506
2015
  let failover
1507
2016
  switch (functionName) {
2017
+ case 'exec':
2018
+ return _exec(data, parameters)
1508
2019
  case 'get':
1509
2020
  if (parameters.length > 2) {
1510
- throw new TemplateException(TEMPLATE_EXCEPTION_TYPE.argumentFormatException)
2021
+ throw new TemplateCompilerException(TEMPLATE_COMPILER_EXCEPTION_TYPE.argumentFormatException)
1511
2022
  }
1512
2023
  if (parameters.length === 2) {
1513
2024
  failover = parameters[parameters.length - 1]
1514
2025
  }
1515
2026
  return _get(data, parameters[0], failover)
1516
- case 'map':
1517
- return _map(data, parameters)
1518
2027
  case 'join':
1519
2028
  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);
2029
+ case 'map':
2030
+ return _map(data, parameters)
2031
+ case 'concatIf':
2032
+ return _concatIf(data, parameters)
1528
2033
  case 'filterOne':
1529
2034
  return _filterOne(data, parameters)
1530
2035
  case 'filterAll':
1531
2036
  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);
2037
+ case 'formatDate':
2038
+ return _formatDate(data, parameters)
1537
2039
  case 'eq':
1538
2040
  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);
2041
+ case 'neq':
2042
+ return _neq(data, parameters)
2043
+ case 'gt':
2044
+ return _gt(data, parameters)
2045
+ case 'gte':
2046
+ return _gte(data, parameters)
2047
+ case 'lt':
2048
+ return _lt(data, parameters)
2049
+ case 'lte':
2050
+ return _lte(data, parameters)
2051
+ case 'isEmpty':
2052
+ return _isEmpty(data, parameters)
2053
+ case 'isNotEmpty':
2054
+ return _isNotEmpty(data, parameters)
2055
+ case 'toLowerCase':
2056
+ return _toLowerCase(data)
2057
+ case 'toUpperCase':
2058
+ return _toUpperCase(data)
1557
2059
  default:
1558
2060
  throw new Error(`${functionName} is not a valid function`)
1559
2061
  }
@@ -1564,7 +2066,7 @@ function _callFunction(data, functionName, parameters) {
1564
2066
 
1565
2067
 
1566
2068
 
1567
- ;// ./lib/models/template/index.js
2069
+ ;// ./lib/models/templateCompiler/index.js
1568
2070
 
1569
2071
 
1570
2072