Browse Source

feat: add `$slice` function

Ahmad Kholid 3 years ago
parent
commit
a1ce2912b6
1 changed files with 8 additions and 0 deletions
  1. 8 0
      src/utils/referenceData/mustacheReplacer.js

+ 8 - 0
src/utils/referenceData/mustacheReplacer.js

@@ -40,6 +40,14 @@ export const functions = {
 
     return value.length ?? value;
   },
+  slice(value, start, end) {
+    if (!value || !value.slice) return value;
+
+    const startIndex = Number.isNaN(+start) ? 0 : +start;
+    const endIndex = Number.isNaN(+end) ? value.length : +end;
+
+    return value.slice(startIndex, endIndex);
+  },
   randData(str) {
     const getRand = (data) => data[Math.floor(Math.random() * data.length)];
     const lowercase = 'abcdefghijklmnopqrstuvwxyz';