diff --git a/README.md b/README.md index acd0d25..2ea8cce 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ This small JS utility reads PHP strings containing arrays and returns a JavaScri It uses [glayzzle/php-parser](https://github.com/glayzzle/php-parser) to parse PHP into AST and uses that info to extract arrays. It supports both indexed and associative arrays (i.e. lists and dictionaries/maps) and array, string, numeric and null values. +Inline function calls are not evaluated but returned as raw strings. See the example below. ## Installation @@ -35,6 +36,7 @@ const phpString = `[ 'and_numeric' => 42, 'what_about' => true, 'or' => false, + 'func' => strtoupper('abc'), ]`; const data = fromString(phpString); ``` @@ -50,7 +52,8 @@ const data = fromString(phpString); also_supports: null, and_numeric: 42, what_about: true, - or: false + or: false, + func: "strtoupper('abc')" } ``` diff --git a/index.js b/index.js index 5a708f0..33e2105 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,10 @@ function fromString (phpString) { extractDoc: false, suppressErrors: true }, - ast: { withPositions: false } + ast: { + withPositions: true, + withSource: true + } }); phpString = phpString.trim(); @@ -72,6 +75,7 @@ function parseValue (expr) { if (expr.kind === 'boolean') return expr.value; if (expr.kind === 'nullkeyword') return null; if (expr.kind === 'identifier' && expr.name.name === 'null') return null; + if (expr.kind === 'call') return expr.loc?.source; return undefined; } diff --git a/index.test.js b/index.test.js index 42479ae..d82b97a 100644 --- a/index.test.js +++ b/index.test.js @@ -57,7 +57,19 @@ test('php file', () => { expect(data).toEqual(['foo', 'bar']); }); -test('all value types match (arrays strings numbers decimals nulls booleans)', () => { +test('function calls', () => { + const data = fromString(`[ + 'func' => env('SOME_VAR', true), + 'another' => substr($var, 0, 5), + 'arrow' => (fn() => 123), + ]`); + expect(data).toEqual({ + func : "env('SOME_VAR', true)", + another: "substr($var, 0, 5)", + }); +}) + +test('all value types match (arrays strings numbers decimals nulls booleans functions)', () => { const data = fromString(`[ 'arrayvalue' => [ 'hello', 'world' @@ -68,6 +80,7 @@ test('all value types match (arrays strings numbers decimals nulls booleans)', ( 'nullvalue' => null, 'truevalue' => true, 'falsevalue' => false, + 'functionvalue' => strtoupper('abc'), ]`); expect(data).toEqual({ arrayvalue: ['hello', 'world'], @@ -76,6 +89,8 @@ test('all value types match (arrays strings numbers decimals nulls booleans)', ( decimalvalue: 4.2, nullvalue: null, truevalue: true, - falsevalue: false + falsevalue: false, + functionvalue: 'strtoupper(\'abc\')' }); }); + diff --git a/package-lock.json b/package-lock.json index c5857a7..ada94d8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "php-array-reader", - "version": "2.0.0", + "version": "2.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "php-array-reader", - "version": "2.0.0", + "version": "2.1.0", "license": "MIT", "dependencies": { "php-parser": "^3.1.5" @@ -1107,9 +1107,9 @@ } }, "node_modules/@types/node": { - "version": "20.5.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.0.tgz", - "integrity": "sha512-Mgq7eCtoTjT89FqNoTzzXg2XvCi5VMhRV6+I2aYanc6kQCBImeNaAYRs/DyoVqk1YEUJK5gN9VO7HRIdz4Wo3Q==", + "version": "20.5.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.1.tgz", + "integrity": "sha512-4tT2UrL5LBqDwoed9wZ6N3umC4Yhz3W3FloMmiiG4JwmUJWpie0c7lcnUNd4gtMKuDEO4wRVS8B6Xa0uMRsMKg==", "dev": true }, "node_modules/@types/stack-utils": { @@ -1379,9 +1379,9 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001521", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001521.tgz", - "integrity": "sha512-fnx1grfpEOvDGH+V17eccmNjucGUnCbP6KL+l5KqBIerp26WK/+RQ7CIDE37KGJjaPyqWXXlFUyKiWmvdNNKmQ==", + "version": "1.0.30001522", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001522.tgz", + "integrity": "sha512-TKiyTVZxJGhsTszLuzb+6vUZSjVOAhClszBr2Ta2k9IwtNBT/4dzmL6aywt0HCgEZlmwJzXJd8yNiob6HgwTRg==", "dev": true, "funding": [ { @@ -1577,9 +1577,9 @@ } }, "node_modules/electron-to-chromium": { - "version": "1.4.494", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.494.tgz", - "integrity": "sha512-KF7wtsFFDu4ws1ZsSOt4pdmO1yWVNWCFtijVYZPUeW4SV7/hy/AESjLn/+qIWgq7mHscNOKAwN5AIM1+YAy+Ww==", + "version": "1.4.496", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.496.tgz", + "integrity": "sha512-qeXC3Zbykq44RCrBa4kr8v/dWzYJA8rAwpyh9Qd+NKWoJfjG5vvJqy9XOJ9H4P/lqulZBCgUWAYi+FeK5AuJ8g==", "dev": true }, "node_modules/emittery": { diff --git a/package.json b/package.json index 971ea8a..1c57fe0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "php-array-reader", - "version": "2.0.0", + "version": "2.1.0", "description": "Reads PHP arrays from a string into a JavaScript object", "main": "index.js", "types": "index.d.ts",