Skip to content

Commit

Permalink
chore(atob): rename atob helper to decodeId
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Metzener authored and czosel committed Jun 6, 2019
1 parent 54788a6 commit eab1d00
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 20 deletions.
24 changes: 16 additions & 8 deletions addon/helpers/atob.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { helper } from "@ember/component/helper";
import { deprecate } from "@ember/application/deprecations";
import { decodeId } from "ember-caluma/helpers/decode-id";

export function atob(str) {
try {
return window.atob(str).split(":")[1];
} catch (e) {
// eslint-disable-next-line no-console
console.warn("Attempted to decode", str, "as base64");
return str;
}
deprecate(
'DEPRECATED: Using the `atob` helper is deprecated. Instead, use the new `decodeId` helper: `import { decodeId } from "ember-caluma/helpers/decode-id";`.',
false,
{ id: "ember-caluma.atob-in-code", until: "1.0.0" }
);

return decodeId(str);
}

export default helper(function([str]) {
return atob(str);
deprecate(
"DEPRECATED: Using the `{{atob}}` helper is deprecated. Instead, use the new `{{decodeId}}` helper.",
false,
{ id: "ember-caluma.atob-in-template", until: "1.0.0" }
);

return decodeId(str);
});
18 changes: 18 additions & 0 deletions addon/helpers/decode-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { helper } from "@ember/component/helper";
import { warn } from "@ember/debug";

export function decodeId(str) {
try {
return window.atob(str).split(":")[1];
} catch (e) {
warn(`Attempted to decode ${str} as base64 but failed`, {
id: "ember-caluma.decode-id"
});

return str;
}
}

export default helper(function([str]) {
return decodeId(str);
});
4 changes: 2 additions & 2 deletions addon/lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { assert } from "@ember/debug";
import { getOwner } from "@ember/application";
import Field from "ember-caluma/lib/field";
import jexl from "jexl";
import { atob } from "ember-caluma/helpers/atob";
import { decodeId } from "ember-caluma/helpers/decode-id";
import { inject as service } from "@ember/service";
import { intersects } from "ember-caluma/utils/jexl";
import { filterBy } from "@ember/object/computed";
Expand Down Expand Up @@ -74,7 +74,7 @@ export default EmberObject.extend({
},

id: computed("raw.id", function() {
return atob(this.get("raw.id"));
return decodeId(this.get("raw.id"));
}),

field: computed(
Expand Down
4 changes: 2 additions & 2 deletions addon/lib/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Evented, { on } from "@ember/object/evented";
import Answer from "ember-caluma/lib/answer";
import Question from "ember-caluma/lib/question";
import Document from "ember-caluma/lib/document";
import { atob } from "ember-caluma/helpers/atob";
import { decodeId } from "ember-caluma/helpers/decode-id";

import saveDocumentFloatAnswerMutation from "ember-caluma/gql/mutations/save-document-float-answer";
import saveDocumentIntegerAnswerMutation from "ember-caluma/gql/mutations/save-document-integer-answer";
Expand Down Expand Up @@ -249,7 +249,7 @@ export default EmberObject.extend(Evented, {
mutation: removeAnswerMutation,
variables: {
input: {
answer: atob(this.get("answer.id"))
answer: decodeId(this.get("answer.id"))
}
}
},
Expand Down
6 changes: 4 additions & 2 deletions addon/mirage-graphql/mocks/task.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import BaseMock from "ember-caluma/mirage-graphql/mocks/base";
import { register } from "ember-caluma/mirage-graphql";
import { classify } from "@ember/string";
import { atob } from "ember-caluma/helpers/atob";
import { decodeId } from "ember-caluma/helpers/decode-id";

export default class extends BaseMock {
@register("Task")
handleTask(root, vars) {
const serialized = this.handle.fn.call(this, root, vars);

let taskId = atob(root.taskId || (root.node && root.node(...arguments).id));
let taskId = decodeId(
root.taskId || (root.node && root.node(...arguments).id)
);
let __typename = root.__typename;

if (taskId) {
Expand Down
4 changes: 2 additions & 2 deletions addon/services/document-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Service from "@ember/service";
import { computed } from "@ember/object";
import { getOwner } from "@ember/application";
import Document from "ember-caluma/lib/document";
import { atob } from "ember-caluma/helpers/atob";
import { decodeId } from "ember-caluma/helpers/decode-id";

/**
* @class DocumentStoreService
Expand All @@ -25,7 +25,7 @@ export default Service.extend({
* @return {Document} The document
*/
find(document, { noCache, parentDocument } = {}) {
const id = atob(document.id);
const id = decodeId(document.id);
const cached = this.documents[id];

if (noCache || !cached) {
Expand Down
1 change: 1 addition & 0 deletions app/helpers/decode-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default, decodeId } from "ember-caluma/helpers/decode-id";
4 changes: 2 additions & 2 deletions tests/dummy/app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { inject as service } from "@ember/service";
import { get } from "@ember/object";
import { RouteQueryManager } from "ember-apollo-client";
import gql from "graphql-tag";
import { atob } from "ember-caluma/helpers/atob";
import { decodeId } from "ember-caluma/helpers/decode-id";
import ENV from "ember-caluma/config/environment";

export default Route.extend(RouteQueryManager, {
Expand Down Expand Up @@ -53,6 +53,6 @@ export default Route.extend(RouteQueryManager, {
"allDocuments.edges"
);

return atob(get(res, "firstObject.node.id"));
return decodeId(get(res, "firstObject.node.id"));
}
});
4 changes: 2 additions & 2 deletions tests/dummy/app/routes/nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { inject as service } from "@ember/service";
import { get } from "@ember/object";
import { RouteQueryManager } from "ember-apollo-client";
import gql from "graphql-tag";
import { atob } from "ember-caluma/helpers/atob";
import { decodeId } from "ember-caluma/helpers/decode-id";

export default Route.extend(RouteQueryManager, {
apollo: service(),
Expand Down Expand Up @@ -34,6 +34,6 @@ export default Route.extend(RouteQueryManager, {
"allDocuments.edges"
);

return atob(get(res, "firstObject.node.id"));
return decodeId(get(res, "firstObject.node.id"));
}
});
19 changes: 19 additions & 0 deletions tests/integration/helpers/decode-id-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { module, test } from "qunit";
import { setupRenderingTest } from "ember-qunit";
import { render } from "@ember/test-helpers";
import hbs from "htmlbars-inline-precompile";

module("Integration | Helper | decodeId", function(hooks) {
setupRenderingTest(hooks);

test("it renders", async function(assert) {
this.set(
"inputValue",
"Q2FzZTo5ZGYzYzYwNy0yZjU0LTQ4YTgtODYzNi1hNDQzNWYyZmI2NTM"
);

await render(hbs`{{decode-id inputValue}}`);

assert.dom(this.element).hasText("9df3c607-2f54-48a8-8636-a4435f2fb653");
});
});

0 comments on commit eab1d00

Please sign in to comment.