Skip to content
This repository has been archived by the owner on Sep 27, 2021. It is now read-only.

Commit

Permalink
added parser for limitTo Angular filter
Browse files Browse the repository at this point in the history
  • Loading branch information
webschik committed Apr 28, 2018
1 parent 21a7d19 commit 0a038b9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
61 changes: 61 additions & 0 deletions __tests__/parser/parse-ng-iterator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import parseNgIterator from '../../src/parser/parse-ng-iterator';

describe('Parser', () => {
describe('parseNgIterator()', () => {
it('should throw an error for invalid expression', () => {
let error: Error;

try {
parseNgIterator('Test expression');
} catch (e) {
error = e;
}

expect(error).toEqual(new Error('Invalid iterator expression \'Test expression\''));
});

it('should throw an error for invalid left-side expression of the iterator', () => {
let error: Error;

try {
parseNgIterator('() in numbers');
} catch (e) {
error = e;
}

expect(error).toEqual(new Error('Invalid left-side expression () in iterator \'() in numbers\''));
});

it('should throw an error for invalid alias in the iterator', () => {
let error: Error;

try {
parseNgIterator('number in numbers as this');
} catch (e) {
error = e;
}

expect(error).toEqual(new Error(
'Iterator alias this is invalid - must be a valid JS identifier which is not a reserved name'
));
});

it('should convert limitTo filter', () => {
expect(parseNgIterator('number in numbers | limitTo:numLimit')).toEqual({
aliasAs: undefined,
collectionIdentifier: 'numbers',
collectionTransform: ['.slice(0, 0 + numLimit)'],
keyIdentifier: undefined,
valueIdentifier: 'number'
});

expect(parseNgIterator('number in numbers | limitTo : numLimit : limitBegin')).toEqual({
aliasAs: undefined,
collectionIdentifier: 'numbers',
collectionTransform: ['.slice(limitBegin, limitBegin + numLimit)'],
keyIdentifier: undefined,
valueIdentifier: 'number'
});
});
});
});
2 changes: 1 addition & 1 deletion npm-shrinkwrap.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
@@ -1,7 +1,7 @@
{
"name": "ng2react-builder",
"description": "Builder of React.js components from Angular1 sources",
"version": "0.6.0",
"version": "0.7.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
Expand Down
21 changes: 14 additions & 7 deletions src/parser/parse-ng-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,45 @@ export default function parseNgIterator (expression: string): AngularIteratorInf
let match: RegExpMatchArray = expression.match(pattern);

if (!match) {
throw new Error(`Invalid iterator expression ${ expression }`);
throw new Error(`Invalid iterator expression '${ expression }'`);
}

const [, iteratorExp, collectionExp, aliasAs]: string[] = match;

match = iteratorExp.match(iteratorExpPattern);

if (!match) {
throw new Error(`Invalid left-side expression ${ iteratorExp } in iterator ${ expression }`);
throw new Error(`Invalid left-side expression ${ iteratorExp } in iterator '${ expression }'`);
}

const valueIdentifier: string = match[3] || match[1];
const keyIdentifier: string = match[2];

if (aliasAs && (!aliasPattern.test(aliasAs) || aliasReservedNamesPattern.test(aliasAs))) {
throw new Error(
`Iterator alias ${ aliasAs } is invalid --- must be a valid JS identifier which is not a reserved name`
`Iterator alias ${ aliasAs } is invalid - must be a valid JS identifier which is not a reserved name`
);
}

const collectionPipe: string[] = collectionExp.split(collectionExpSeparator);
const collectionTransform: string[] = [];

collectionPipe.slice(1).forEach((transform: string) => {
const [name, value] = transform.split(':');
const ngFilterData: string[] = transform.split(':');
const ngFilterName: string = ngFilterData[0].trim();

switch (name) {
switch (ngFilterName) {
case 'filter':
collectionTransform.push(`.filter(${ value })`);
collectionTransform.push(`.filter(${ ngFilterData[1] })`);
break;
case 'orderBy':
collectionTransform.push(`.sort(${ value })`);
collectionTransform.push(`.sort(${ ngFilterData[1] })`);
break;
case 'limitTo':
const limit: string = (ngFilterData[1] || '').trim();
const begin: string = (ngFilterData[2] || '').trim();

collectionTransform.push(`.slice(${ begin || 0 }, ${ begin || 0 } + ${ limit || 0 })`);
break;
default:
//
Expand Down

0 comments on commit 0a038b9

Please sign in to comment.