languages | Cell 9 | Cell 11 | Search

The given code creates a mapping between PHP and JavaScript functions using two main objects, fromPhpToJavascript and fromJavascriptToPhp, which contain key-value pairs of function names and their corresponding implementations in the other language. The phpToJs array maps PHP functions to their JavaScript equivalents, and the jsToPhp array is created by reversing the elements of each inner array in phpToJs for the inverse mapping.

Cell 10


// make sure this format works both directions
// from PHP to javascript
var fromPhpToJavascropt = {
    '//CallExpression[@name="array_shift"]': {
        
    }
}

var fromJavascriptToPhp = {
    
}

var phpToJs = [
    [() => array_shift(arr), () => arr.shift()],
    [() => strpos(str, match), () => { // has parameters so call this function instead?
        var i = str.indexOf(match);
        return i > -1 ? i : false}],
    [() => substr(str, start, length), () => str.substr(start, length)],
    [() => is_numeric(num), () => !isNaN(parseInt(num)) || !isNaN(parseFloat(num))],
    [() => abs(), () => Math.abs()], // doesn't mess with parameters
    // Call Identifier name match done automatically
    //[['abs', 'sin', 'cos', 'pow', 'floor', 'ceil', 'round', 'max', 'min'], {
    //    `//*[contains(@type, "Call")`: (id) => Math.id(), // optional ()
    //}]
    // above is shorthand for this:
    [(id = ['abs', 'sin', 'cos',
            'pow', 'floor', 'ceil',
            'round', 'max', 'min']) => id(), {
                '//*[contains(@type, "Call")': (id) => Math.id(), // optional ()
    }],

]

// this will work as long as PHP syntax is also called a Call or Member or Indentifier
var jsToPhp = phpToJs.map(d => [d[1], d[0]])

What the code could have been:

/**
 * Mapping of PHP functions to JavaScript equivalents.
 * @typedef {Object} PhpJsMapping
 * @property {string} php - PHP function name
 * @property {string} js - JavaScript equivalent
 */

/**
 * Mapping of PHP functions to JavaScript equivalents.
 * @type {PhpJsMapping[]}
 */
const phpToJs = [
  {
    php: '//CallExpression[@name="array_shift"]',
    js: { name: 'arrayShift', fn: arr => arr.shift() }
  },
  {
    php: '//CallExpression[@name="strpos"]',
    js: { name:'strpos', fn: (str, match) => str.indexOf(match)!== -1? str.indexOf(match) : false }
  },
  {
    php: '//CallExpression[@name="substr"]',
    js: { name:'substr', fn: (str, start, length) => str.substr(start, length) }
  },
  {
    php: '//CallExpression[@name="is_numeric"]',
    js: { name: 'isNumeric', fn: num =>!isNaN(parseInt(num)) ||!isNaN(parseFloat(num)) }
  },
  {
    php: '//CallExpression[@name="abs"]',
    js: { name: 'abs', fn: () => Math.abs() }
  }
];

/**
 * Shorthand mapping for built-in math functions.
 * @type {Object}
 */
const mathFunctions = {
  ['abs','sin', 'cos', 'pow', 'floor', 'ceil', 'round','max','min']: (id) => Math[id]()
};

// Combine PHP to JavaScript mapping with shorthand math functions.
const combinedPhpToJs = [...phpToJs,...Object.entries(mathFunctions).map(([id, fn]) => ({ php: `//CallExpression[@name="${id}"]`, js: { name: id, fn } }))];

// Reverse the mapping for JavaScript to PHP.
const jsToPhp = combinedPhpToJs.map(item => ({ js: item.php, php: item.js.name }));

// Output combined JavaScript to PHP mapping.
console.log(jsToPhp);

Code Breakdown

Overview

The code is a mapping between PHP and JavaScript functions. It consists of two main objects, fromPhpToJavascript and fromJavascriptToPhp, which contain key-value pairs of function names and their corresponding implementations in the other language.

Mapping Variables

Example Mappings

Some examples from the phpToJs array:

Additional Notes