-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
adjust
function (issue #26)
- Loading branch information
Showing
5 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
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
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
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,29 @@ | ||
import { Request } from './_types' | ||
|
||
/** | ||
* Recursively merge requests | ||
*/ | ||
const merge = <RequestEx extends Request>(request: RequestEx, modify: Partial<Request>) => { | ||
const result = { ...request, ...modify } | ||
|
||
for (const key in modify) { | ||
if ( | ||
modify[key] && | ||
request[key] && | ||
typeof modify[key] === 'object' && | ||
typeof request[key] === 'object' | ||
) { | ||
result[key] = merge(request[key], modify[key]) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* Adjust any Request *object* fields, for any request | ||
* @return new typed Request, doesn't modify original Request | ||
*/ | ||
export const adjust: { | ||
<RequestEx extends Request>(modify: Partial<Request>): (request: RequestEx) => RequestEx | ||
} = (modify) => (request) => merge(request, modify) |
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
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,59 @@ | ||
import { adjust, Request, RequestType } from '../src' | ||
|
||
// dumb object to test purity | ||
const dumb: Request = { | ||
type: RequestType.Undefined, | ||
url: 'test', | ||
fields: { | ||
scale: 1, | ||
landscape: true, | ||
pageRanges: '1-2', | ||
}, | ||
client: { | ||
post: () => { | ||
throw new Error('not implemented') | ||
}, | ||
}, | ||
} | ||
|
||
test('Should adjust flat fields', () => { | ||
expect(adjust({})(dumb)).toEqual({ ...dumb }) | ||
expect(adjust({ url: 'changed' })(dumb)).toEqual({ ...dumb, url: 'changed' }) | ||
}) | ||
|
||
test('Should adjust deep fields', () => { | ||
expect(adjust({ fields: { landscape: false } })(dumb)).toEqual({ | ||
...dumb, | ||
fields: { | ||
...dumb.fields, | ||
landscape: false, | ||
}, | ||
}) | ||
expect(adjust({ headers: { Authorization: 'Bearer token' } })(dumb)).toEqual({ | ||
...dumb, | ||
headers: { | ||
Authorization: 'Bearer token', | ||
}, | ||
}) | ||
expect( | ||
adjust({ headers: { Authorization: 'Bearer token' } })({ | ||
...dumb, | ||
headers: { 'X-Header': 'test' }, | ||
}) | ||
).toEqual({ | ||
...dumb, | ||
headers: { | ||
Authorization: 'Bearer token', | ||
'X-Header': 'test', | ||
}, | ||
}) | ||
}) | ||
|
||
test('Should replace deep fields', () => { | ||
expect( | ||
adjust({ headers: { Authorization: 'Bearer token' } })({ | ||
...dumb, | ||
headers: { Authorization: 'Basic dXNlcjpwYXNzd29yZA==' }, | ||
}) | ||
).toEqual({ ...dumb, headers: { Authorization: 'Bearer token' } }) | ||
}) |