Skip to content

Commit

Permalink
feat(amplify-appsync-simulator): implement new utils (#6257)
Browse files Browse the repository at this point in the history
feat(amplify-appsync-simulator): implement new utils
  • Loading branch information
bboure committed Jan 7, 2021
1 parent 2de691a commit 1c1dda9
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { create } from '../../../velocity/util/index';
import { GraphQLResolveInfo } from 'graphql';
import { map, random } from 'lodash';

const stubInfo = {} as unknown;
export const mockInfo = stubInfo as GraphQLResolveInfo;
var util;

beforeEach(() => {
util = create(undefined, undefined, mockInfo);
});

describe('$utils.list.copyAndRetainAll', () => {
it('should retain', () => {
const myList = [1, 2, 3, 4, 5];
expect(util.list.copyAndRetainAll(myList, [2, 4])).toEqual([2, 4]);
});
});

describe('$utils.list.copyAndRemoveAll', () => {
it('should remove', () => {
const myList = [1, 2, 3, 4, 5];
expect(util.list.copyAndRemoveAll(myList, [2, 4])).toEqual([1, 3, 5]);
});
});

describe('$utils.list.sortList', () => {
it('should sort a list of objects asc', () => {
const myList = [
{ description: 'youngest', age: 5 },
{ description: 'middle', age: 45 },
{ description: 'oldest', age: 85 },
];
expect(util.list.sortList(myList, false, 'description')).toEqual([
{ description: 'middle', age: 45 },
{ description: 'oldest', age: 85 },
{ description: 'youngest', age: 5 },
]);
});

it('should sort a list of objects desc', () => {
const myList = [
{ description: 'youngest', age: 5 },
{ description: 'middle', age: 45 },
{ description: 'oldest', age: 85 },
];
expect(util.list.sortList(myList, true, 'description')).toEqual([
{ description: 'youngest', age: 5 },
{ description: 'oldest', age: 85 },
{ description: 'middle', age: 45 },
]);
});

it('should sort a list of strings asc', () => {
const myList = ['youngest', 'middle', 'oldest'];
expect(util.list.sortList(myList, false, 'any')).toEqual(['middle', 'oldest', 'youngest']);
});

it('should sort a list of strings desc', () => {
const myList = ['youngest', 'middle', 'oldest'];
expect(util.list.sortList(myList, true, 'any')).toEqual(['youngest', 'oldest', 'middle']);
});

it('should sort a list of integers asc', () => {
const myList = [10, 1, 5];
expect(util.list.sortList(myList, false, 'any')).toEqual([1, 5, 10]);
});

it('should sort a list of integers desc', () => {
const myList = [10, 1, 5];
expect(util.list.sortList(myList, true, 'any')).toEqual([10, 5, 1]);
});

it('should not sort mixed content', () => {
const myList = [{ name: 'foo' }, 1, 'bar'];
expect(util.list.sortList(myList, true, 'any')).toEqual(myList);
});

it('should not sort list > 1000 elements', () => {
const myList = map(Array(1100), () => random(0, 100));
expect(util.list.sortList(myList, true, 'any')).toEqual(myList);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { create } from '../../../velocity/util/index';
import { GraphQLResolveInfo } from 'graphql';

const stubInfo = {} as unknown;
export const mockInfo = stubInfo as GraphQLResolveInfo;
var util;

beforeEach(() => {
util = create(undefined, undefined, mockInfo);
});

describe('$utils.math.round', () => {
it('should round a double', () => {
expect(util.math.roundNum(10.2)).toEqual(10);
expect(util.math.roundNum(10.8)).toEqual(11);
expect(util.math.roundNum(10)).toEqual(10);
});
});

describe('$utils.math.minVal', () => {
it('should get the min value', () => {
expect(util.math.minVal(13.45, 45.67)).toEqual(13.45);
});
});

describe('$utils.math.maxVal', () => {
it('get the max value', () => {
expect(util.math.maxVal(13.45, 45.67)).toEqual(45.67);
});
});

describe('$utils.math.random', () => {
it('get a random value', () => {
expect(typeof util.math.randomDouble()).toBe('number');
});
});

describe('$utils.math.randomWithinRange', () => {
it('get a randomWithinRange value', () => {
expect(typeof util.math.randomWithinRange(10, 20)).toBe('number');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { create } from '../../../velocity/util/index';
import { GraphQLResolveInfo } from 'graphql';

const stubInfo = {} as unknown;
export const mockInfo = stubInfo as GraphQLResolveInfo;
var util;

beforeEach(() => {
util = create(undefined, undefined, mockInfo);
});

describe('$utils.str.toLower', () => {
it('should chnage a string to lowercase', () => {
expect(util.str.toLower('HELLO WORLD')).toEqual('hello world');
expect(util.str.toLower('hello world')).toEqual('hello world');
expect(util.str.toLower('HeLlo WorlD')).toEqual('hello world');
});
});

describe('$utils.str.toUpper', () => {
it('should chnage a string to uppercase', () => {
expect(util.str.toUpper('HELLO WORLD')).toEqual('HELLO WORLD');
expect(util.str.toUpper('hello world')).toEqual('HELLO WORLD');
expect(util.str.toUpper('HeLlo WorlD')).toEqual('HELLO WORLD');
});
});

describe('$utils.str.toReplace', () => {
it('should replace a string', () => {
expect(util.str.toReplace('hello world, hello!', 'hello', 'mellow')).toEqual('mellow world, mellow!');
});
});

describe('$utils.str.normalize', () => {
it('should normalize a string', () => {
expect(util.str.normalize('\u0041\u006d\u0065\u0301\u006c\u0069\u0065', 'nfc')).toEqual('Amélie');
expect(util.str.normalize('\u0041\u006d\u0065\u0301\u006c\u0069\u0065', 'nfc')).toEqual('Amélie');
expect(util.str.normalize('\u00F1', 'nfd')).toEqual('ñ');
expect(util.str.normalize('\u006E\u0303', 'nfd')).toEqual('ñ');
});
});
4 changes: 4 additions & 0 deletions packages/amplify-appsync-simulator/src/velocity/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { listUtils } from './list-utils';
import { mapUtils } from './map-utils';
import { transformUtils } from './transform';
import { time } from './time';
import { str } from './str';
import { math } from './math';
import { GraphQLResolveInfo } from 'graphql';

export function create(errors = [], now: Date = new Date(), info: GraphQLResolveInfo) {
Expand All @@ -18,5 +20,7 @@ export function create(errors = [], now: Date = new Date(), info: GraphQLResolve
errors,
info,
time: time(),
str,
math,
};
}
18 changes: 18 additions & 0 deletions packages/amplify-appsync-simulator/src/velocity/util/list-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import { identity, isObject, negate, orderBy, some } from 'lodash';

export const listUtils = {
copyAndRetainAll(list: any[], intersect: any[]) {
return list.filter(value => intersect.indexOf(value) !== -1);
},
copyAndRemoveAll(list: any[], toRemove: any[]) {
return list.filter(value => toRemove.indexOf(value) === -1);
},
sortList(list: any[], desc: boolean, property: string) {
if (list.length === 0 || list.length > 1000) {
return list;
}

const type = typeof list[0];
const isMixedTypes = some(list.slice(1), i => typeof i !== type);

if (isMixedTypes) {
return list;
}

const isScalarList = some(list, negate(isObject));

return orderBy(list, isScalarList ? identity : property, desc ? 'desc' : 'asc');
},
};
9 changes: 9 additions & 0 deletions packages/amplify-appsync-simulator/src/velocity/util/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { random } from 'lodash';

export const math = {
roundNum: Math.round,
minVal: Math.min,
maxVal: Math.max,
randomDouble: Math.random,
randomWithinRange: random,
};
14 changes: 14 additions & 0 deletions packages/amplify-appsync-simulator/src/velocity/util/str.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const str = {
toUpper(str: string) {
return str.toUpperCase();
},
toLower(str: string) {
return str.toLowerCase();
},
toReplace(str: string, substr: string, newSubstr: string) {
return str.replace(new RegExp(substr, 'g'), newSubstr);
},
normalize(str: string, form: string) {
return str.normalize(form.toUpperCase());
},
};

0 comments on commit 1c1dda9

Please sign in to comment.