Skip to content

Commit

Permalink
Merge pull request #632 from pomm0/chore/619-remove-jquery
Browse files Browse the repository at this point in the history
#619 chore: get rid of `$.param`
  • Loading branch information
ro0gr authored Jan 13, 2024
2 parents 35ab147 + bd924c1 commit aac1b79
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Inside any of the packages you can run:

- `cd addon && pnpm start` – Builds the addon in "watch mode" so changes picked up by test app.
- `pnpm test` – Runs the test suite on the current Ember version
- `pnpm test --server` – Runs the test suite in "watch mode"
- `pnpm test -- --server` – Runs the test suite in "watch mode"
- `cd test-app && ember try:each` – Runs the test suite against multiple Ember versions

## Running the test application
Expand Down
44 changes: 41 additions & 3 deletions addon/src/properties/visitable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { $ } from '../-private/jquery';
import action from '../-private/action';
import { getAdapter } from '../adapters/index';

Expand Down Expand Up @@ -27,9 +26,48 @@ function fillInDynamicSegments(path, params) {
.join('/');
}

function addValue(
urlSearchParams,
key,
value,
parentKey = '',
isArrayValue = false
) {
let keyWithParent = parentKey ? `${parentKey}[${key}]` : key;

if (Array.isArray(value)) {
// array
value.forEach((arrayItem) =>
addValue(urlSearchParams, key, arrayItem, parentKey, true)
);
} else if (typeof value === 'object' && value !== null) {
// object
Object.keys(value).forEach((_key) =>
addValue(urlSearchParams, _key, value[_key], keyWithParent)
);
} else {
// primitive
if (isArrayValue) {
urlSearchParams.append(`${keyWithParent}[]`, value);
} else {
urlSearchParams.append(keyWithParent, value);
}
}

return urlSearchParams;
}

function appendQueryParams(path, queryParams) {
if (Object.keys(queryParams).length) {
path += `?${$.param(queryParams)}`;
let keys = Object.keys(queryParams);

if (keys.length) {
let urlSearchParams = keys.reduce(
(urlSearchParams, key) =>
addValue(urlSearchParams, key, queryParams[key]),
new URLSearchParams()
);

path += `?${urlSearchParams}`;
}

return path;
Expand Down
26 changes: 24 additions & 2 deletions test-app/tests/unit/-private/properties/visitable-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,30 @@ module('visitable', function(hooks) {
foo: visitable('/users/:user_id/comments/:comment_id')
});

await page.foo({ user_id: 5, comment_id: 1, hello: 'world', lorem: 'ipsum' });
assert.equal(currentURL(), '/users/5/comments/1?hello=world&lorem=ipsum');
await page.foo({
user_id: 5,
comment_id: 1,
hello: 'world',
lorem: 'ipsum',
reply_ids: [1, 2],
search: {
author: 'ember',
topic: 'lts',
deep: {
my_array: [99, 77],
my_value: true
}
}
});

assert.equal(
decodeURIComponent(currentURL()),
'/users/5/comments/1' +
'?hello=world&lorem=ipsum' +
'&reply_ids[]=1&reply_ids[]=2' +
'&search[author]=ember&search[topic]=lts' +
'&search[deep][my_array][]=99&search[deep][my_array][]=77&search[deep][my_value]=true'
);
});

test('fills in encoded dynamic segments', async function(assert) {
Expand Down

0 comments on commit aac1b79

Please sign in to comment.