-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Use Rails formed URL search params
- Loading branch information
Showing
5 changed files
with
77 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { railsQueryString } from './rails-querystring'; | ||
|
||
describe('railsQueryString', () => { | ||
it('encodes null', () => { | ||
const result = railsQueryString.stringify(null); | ||
expect(result).toBe(''); | ||
}); | ||
|
||
it('encodes an empty object', () => { | ||
const result = railsQueryString.stringify({}); | ||
expect(result).toBe(''); | ||
}); | ||
|
||
it('encodes a basic record', () => { | ||
const result = railsQueryString.stringify({ key: 'value' }); | ||
expect(result).toBe('key=value'); | ||
}); | ||
|
||
it('encodes a record with multiple values', () => { | ||
const result = railsQueryString.stringify({ | ||
key1: 'value1', | ||
key2: 'value2', | ||
}); | ||
expect(result).toBe('key1=value1&key2=value2'); | ||
}); | ||
|
||
it('encodes an array inside a record', () => { | ||
const result = railsQueryString.stringify({ | ||
key1: 'value1', | ||
key2: 'value2', | ||
key3: ['apple', 'facebook', 'microsoft'], | ||
}); | ||
expect(result).toBe( | ||
'key1=value1&key2=value2&key3[]=apple&key3[]=facebook&key3[]=microsoft', | ||
); | ||
}); | ||
}); |
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,30 @@ | ||
import { isObject } from './is-object'; | ||
|
||
/** | ||
* Encodes URI in Rails format | ||
*/ | ||
const stringify = (object?: unknown): string => { | ||
if (object == null) { | ||
return ''; | ||
} | ||
if (!isObject(object)) { | ||
return ''; | ||
} | ||
|
||
const values = Object.entries(object) | ||
.reduce<string[]>((prev, [k, v]) => { | ||
if (Array.isArray(v)) { | ||
const xs = v.map((x) => `${k}[]=${x}`); | ||
return prev.concat(...xs); | ||
} | ||
if (isObject(v)) { | ||
throw new TypeError('Encoding nested object is not supported'); | ||
} | ||
return prev.concat(`${k}=${v}`); | ||
}, []) | ||
.join('&'); | ||
|
||
return values; | ||
}; | ||
|
||
export const railsQueryString = { stringify }; |
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