javascript - How can I use higher order functions like Array.reduce() in an Indesign script? -


i've started project need use adobe indesign , extendscript programmatically extract data series of indd files. version of javascript used scripting in these programs doesn't support of higher order functions i'm used using (array.reduce(), array.foreach(), array.map(), etc...).

is there way add functionality extendscript? feel i'm walking around in room 4 foot high ceiling.

use polyfill

extendscript appears support prototyping of pure javascript objects (but not indesign dom objects), possible use polyfill add missing functionality. polyfill code can found on mdn on page method in question under "polyfill". here's example: mdn array.prototype.reduce() polyfill. there polyfills numerous methods, including array.map(), array.indexof(), array.filter(), , array.foreach().

to implement code, create appropriately named file (ie, polyfill.js or reduce.js) in same folder script. copy polyfill code mdn file created, so:

// production steps of ecma-262, edition 5, 15.4.4.21 // reference: http://es5.github.io/#x15.4.4.21 if (!array.prototype.reduce) {   array.prototype.reduce = function(callback /*, initialvalue*/) {     'use strict';     if (this == null) {       throw new typeerror('array.prototype.reduce called on null or undefined');     }     if (typeof callback !== 'function') {       throw new typeerror(callback + ' not function');     }     var t = object(this), len = t.length >>> 0, k = 0, value;     if (arguments.length == 2) {       value = arguments[1];     } else {       while (k < len && !(k in t)) {         k++;        }       if (k >= len) {         throw new typeerror('reduce of empty array no initial value');       }       value = t[k++];     }     (; k < len; k++) {       if (k in t) {         value = callback(value, t[k], k, t);       }     }     return value;   }; } 

then add following line @ beginning of script, replacing filename appropriately:

#include 'polyfill.js'; 

the semicolon @ end of line isn't included in adobe documentation, find extendscript throws error if it's left off, if you're using #include multiple times.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -