Skip to content

Uniqueness in arrays with simple datatype #14

@SpinGit

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.

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