-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
426 additions
and
641 deletions.
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
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
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,27 @@ | ||
import { hasValue, isObject } from '@tg-resources/is'; | ||
|
||
// Needle defaults to {value} or ${value} | ||
const needle = /\$?{([\s\S]+?)}/g; | ||
|
||
function compileURL(template: string) { | ||
const render = (params: Record<string, unknown>) => { | ||
return template.replace(needle, (_, key: any) => { | ||
let value: Record<string, unknown> | unknown = params; | ||
let itemKey = key; | ||
|
||
while (itemKey.includes('.')) { | ||
const curKey: string = itemKey.split('.')[0]; | ||
value = isObject(value) ? value?.[curKey] : undefined; | ||
|
||
itemKey = itemKey.split('.').slice(1).join('.'); | ||
} | ||
|
||
const finalValue = isObject(value) ? value[itemKey] : ''; | ||
return hasValue(finalValue) ? `${finalValue}` : ''; | ||
}); | ||
}; | ||
|
||
return render; | ||
} | ||
|
||
export default compileURL; |
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
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,56 @@ | ||
import { isRouteTemplate, routeTemplate } from '../src'; | ||
import { cleanRoot, cleanRoute } from '../src/utils'; | ||
|
||
describe('cleanRoot', () => { | ||
test('cleanRoot works', () => { | ||
expect(cleanRoot('/')).toBe(''); | ||
expect(cleanRoot('/test')).toBe('/test'); | ||
expect(cleanRoot('/test/')).toBe('/test'); | ||
expect(cleanRoot('/test/path')).toBe('/test/path'); | ||
expect(cleanRoot('/test/path/')).toBe('/test/path'); | ||
}); | ||
}); | ||
|
||
describe('cleanRoute', () => { | ||
test('cleanRoute works', () => { | ||
expect(cleanRoute('/')).toBe(''); | ||
expect(cleanRoute('/test')).toBe('test'); | ||
expect(cleanRoute('/test/')).toBe('test/'); | ||
expect(cleanRoute('/test/path')).toBe('test/path'); | ||
expect(cleanRoute('/test/path/')).toBe('test/path/'); | ||
}); | ||
}); | ||
|
||
describe('isRouteTemplate', () => { | ||
test('happy path', () => { | ||
const method = routeTemplate('/test/path'); | ||
|
||
expect(isRouteTemplate(method)).toBe(true); | ||
}); | ||
|
||
test('not function', () => { | ||
const method = 'not a function'; | ||
|
||
expect(isRouteTemplate(method)).toBe(false); | ||
}); | ||
|
||
test('missing routePath', () => { | ||
const method = () => {}; | ||
|
||
expect(isRouteTemplate(method)).toBe(false); | ||
}); | ||
|
||
test('routepath is not a string', () => { | ||
const method = routeTemplate('/test/path'); | ||
(method as any).routePath = 1; | ||
|
||
expect(isRouteTemplate(method)).toBe(false); | ||
}); | ||
|
||
test('missing configure', () => { | ||
const method = routeTemplate('/test/path'); | ||
delete (method as any).configure; | ||
|
||
expect(isRouteTemplate(method)).toBe(false); | ||
}); | ||
}); |
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
Oops, something went wrong.