Skip to content

Uniqueness in arrays with simple datatype #14

Description

@SpinGit

Thank you very much for the nestedObjectAssign function. It helps me to update data into aws dynamodb.

describe("array object", () => {

it("should add text to empty comments array", () => {
  const dbData = {comments: []}
  const requestData = {comments: ["foo"]}

  nestedObjectAssign(dbData, requestData);
  expect(dbData).toStrictEqual({comments: ["foo"]});
});

it("should NOT add text duplicates", () => {
  const dbData = {comments: ["foo"]}
  const requestData = {comments: ["foo"]}

  nestedObjectAssign(dbData, requestData);
  expect(dbData).toStrictEqual({comments: ["foo"]});
});

it("should NOT change existing properties in db, if they not updated via request", () => {
  const dbData = {comments: ["bar", "foo"]}
  const requestData = {comments: ["foo"]}

  nestedObjectAssign(dbData, requestData);
  expect(dbData).toStrictEqual({comments: ["bar", "foo"]});
});

});

To realize green tests i changed the function to typescript and
changed line 23 to target[key] = [...new Set([...target[key], ...source[key]])]

Hopefully it helps someone.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions