Skip to content

Commit

Permalink
feat: support Array.prototype.sort, close#89
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Nov 24, 2019
1 parent 44ff034 commit 9a59438
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 66 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ catch (\Exception $e) {
- Array.prototype.every
- Array.prototype.find
- Array.prototype.findIndex
- Array.prototype.sort
- Number
- Number.isInterger
- Number.prototype.toFixed
Expand Down
161 changes: 99 additions & 62 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"source-map-support": "^0.5.12",
"sync-files": "^1.0.3",
"ts-node": "^8.2.0",
"typedoc": "^0.14.2",
"typedoc": "^0.15.3",
"typescript": "~3.4.5",
"vue": "^2.6.10"
},
Expand Down
25 changes: 22 additions & 3 deletions src/features/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,26 @@ import {
import method, {formatMethodName} from '../utilities/method';
import ts = require('typescript');

function sort(node: CallExpression, {emitExpressionList, writePunctuation}, {typeChecker}) {

function sort(node: CallExpression, {emitExpressionList, writePunctuation, errors}, {typeChecker}) {
let expNode = node.expression as PropertyAccessExpression;
let args = [expNode.expression];
if (!node.arguments || node.arguments.length <= 0) {
writePunctuation('sort');
emitExpressionList(node, createNodeArray(args), ListFormat.CallExpressionArguments);
}
else if (
ts.isArrowFunction(node.arguments[0])
|| ts.isFunctionExpression(node.arguments[0])
) {
writePunctuation('usort');
emitExpressionList(node, createNodeArray([...args, node.arguments[0]]), ListFormat.CallExpressionArguments);
}
else {
errors.push({
code: 1,
msg: `Array.prototype.sort does not support parameters except arrow function or function expression`
});
}
}


Expand Down Expand Up @@ -69,7 +87,8 @@ const map = {
filter: method('array_filter', true, 1),
slice: method('%helper::arraySlice', true, 2),
find: method('%helper::array_find', false, 1, true),
findIndex: method('%helper::array_find_index', false, 1, true)
findIndex: method('%helper::array_find_index', false, 1, true),
sort
};

const api = {
Expand Down
4 changes: 4 additions & 0 deletions test/features/ArrayApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ function run($x) {
$zz = \Ts2Php_Helper::array_find_index(function ($item) {
return $item === "a";
}, $b);
$y = sort($b);
$yy = usort($b, function ($a, $b){
return $a - $b;
});
3 changes: 3 additions & 0 deletions test/features/ArrayApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ const z = b.find(item => {
const zz = b.findIndex(item => {
return item === 'a';
});

const y = b.sort();
const yy = b.sort((a: number, b: number) => a - b);

0 comments on commit 9a59438

Please sign in to comment.