-
Notifications
You must be signed in to change notification settings - Fork 816
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(amplify-appsync-simulator): implement new utils (#6257)
feat(amplify-appsync-simulator): implement new utils
- Loading branch information
Showing
7 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
packages/amplify-appsync-simulator/src/__tests__/velocity/util/list-utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
packages/amplify-appsync-simulator/src/__tests__/velocity/util/math.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
packages/amplify-appsync-simulator/src/__tests__/velocity/util/str.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('ñ'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/amplify-appsync-simulator/src/velocity/util/list-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
packages/amplify-appsync-simulator/src/velocity/util/str.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}, | ||
}; |