Browse Source

fix: `$last` path return `undefined` in javascript block

Ahmad Kholid 3 years ago
parent
commit
5fbed3d1c0
1 changed files with 4 additions and 0 deletions
  1. 4 0
      src/content/blocks-handler/handler-javascript-code.js

+ 4 - 0
src/content/blocks-handler/handler-javascript-code.js

@@ -20,6 +20,10 @@ function findData(obj, path) {
   const paths = path.split('.');
   const isWhitespace = paths.length === 1 && !/\\S/.test(paths[0]);
 
+  if (path.startsWith('$last') && Array.isArray(obj)) {
+    paths[0] = obj.length - 1;
+  }
+
   if (paths.length === 0 || isWhitespace) return obj;
   else if (paths.length === 1) return obj[paths[0]];