Detalhes do pacote

mixme

adaltas4.6mMIT2.0.2

A library for recursively merging JavaScript objects

clone, copy, deep, extend

readme (leia-me)

Node.js mixme

Build Status

Merge multiple object recursively, with TypeScript support. The last object takes precedence over the previous ones. Only objects are merged. Arrays are overwritten.

  • Zero dependencies
  • Small size
  • Pure functions
  • ESM and CommonJS support

API

The API is minimalist. The most popular functions are merge, mutate and is_object_literal.

Function camelize(object)

Clone a object and convert its properties into snake case.

import { snake_case } from "mixme"

snake_case({aA: "1", bB: cC: "2"})
// Return {a_a: "1", b_b: c_c: "2"}

Function camelize_str(str)

Convert a camel case string to snake case, used internally by snake_case.

import { snake_case_str } from "mixme";

snake_case("myValue");
// Return "my_value"

Function compare(item_1, item_2)

Compare two items and return true if their values match.

import { compare } from "mixme";

compare([{ a: 1 }], [{ a: 1 }]);
// Return true

compare({ a: 1 }, { a: 2 });
// Return false

Function clone(data)

It is possible to clone a literal object by simply calling mixme with this object as the first argument. Use the clone function in case you wish to clone any type of argument including arrays:

import { clone } from "mixme";

const target = clone(["a", "b"]);
// target is now a copy of source

Function is_object_literal(object)

Use the is_object_literal function to ensure an object is literate.

import { is_object_literal } from "mixme";

// {} is literate
is_object_literal({});

// error is not literate
is_object_literal(new Error("Catch me"));

// Array is not literate
is_object_literal([]);

Function merge(...data)

The API is minimalist, Merge all literal object provided as arguments. This function is immutable, the source objects won't be altered.

import { merge } from "mixme";

const target = merge({ a: "1" }, { b: "2" });
// target is {a: "1", b: "2"}

Function mutate(...data)

Use the mutate function to enrich an object. The first argument will be mutated:

import { mutate } from "mixme";

const source = { a: "1" };
const target = mutate(source, { b: "2" });
target.c = "3";
// source and target are both {a: "1", b: "2", c: "3"}

Function snake_case(object)

Clone a object and convert its properties into snake case.

import { snake_case } from "mixme"

snake_case({aA: "1", bB: cC: "2"})
// Return {a_a: "1", b_b: c_c: "2"}

Function snake_case_str(str)

Convert a camel case string to snake case, used internally by snake_case.

import { snake_case_str } from "mixme";

snake_case("myValue");
// Return "my_value"

Example

Create a new object from two objects:

import { merge } from "mixme";

const obj1 = { a_key: "a value", b_key: "b value" };
const obj2 = { b_key: "new b value" };
const result = merge(obj1, obj2);

assert.eql(result.b_key, "new b value");

Merge an existing object with a second one:

import { mutate } from "mixme";

const obj1 = { a_key: "a value", b_key: "b value" };
const obj2 = { b_key: "new b value" };
const result = mutate(obj1, obj2);

assert.eql(result, obj1);
assert.eql(obj1.b_key, "new b value");

Testing

Clone the repo, install the development dependencies and run the tests:

git clone http://github.com/wdavidw/node-mixme.git .
npm install
npm run test

Developers

To automatically generate a new version:

npm run release

Package publication is handled by the CI/CD with GitHub action.

Note:

  • On release, both the publish and test workflows run in parallel. Not very happy about it but I haven't found a better way.

Contributors

This package is developed by Adaltas.

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

2.0.2 (2025-02-19)

2.0.1 (2025-02-04)

2.0.0 (2025-01-28)

⚠ BREAKING CHANGES

  • typescript conversion and umd bundle removal

Features

  • typescript conversion and umd bundle removal (84ce43d)

Bug Fixes

  • snake_case is_object type predicate (29bb49d)

1.1.0 (2023-11-19)

Features

  • new camelize and camelize_str functions (7d7d3c9)

1.0.0 (2023-11-18)

0.5.10 (2023-11-18)

Features

0.5.9 (2023-03-16)

Bug Fixes

0.5.8 (2023-03-16)

Bug Fixes

  • publish lib folder to npm (0fa53e2)

0.5.7 (2023-03-16)

Bug Fixes

  • publish lib folder to npm (0fa53e2)

0.5.6 (2023-03-16)

Features

  • add typescript support (dbc168f)
  • migrate from coffee to js (44d13c0)

0.5.5 (2023-01-20)

0.5.4 (2021-09-21)

Bug Fixes

  • remove constructor from excluded properties (cf6af53)

0.5.3 (2021-09-21)

Bug Fixes

  • relax properties excluded from copy (d22c51a)

Version 0.5.2

  • fix: prevent code injection in copying properties

Version 0.5.1

  • fix: dont pollute object proto #1
  • chore: latest dependencies

Version 0.5.0

  • feat: support object with null prototype

Version 0.4.0

  • compare: new function

Version 0.3.5

  • readme: fix examples

Version 0.3.3

  • package: update adaltas url

Version 0.3.2

  • package: use files instead of npmignore

Version 0.3.1

  • mutate: immutable deep copy of object in arrays

Version 0.3.0

  • project: csj, esm and umd generation
  • project: ignore lock files

Version 0.2.0

  • snake_case: clone object with converted properties
  • merge: fix array and object merge
  • is_object_literal: new function

Version 0.1.0

  • clone: new function
  • readme: add author company

Version 0.1.0

  • package: support node version above 6
  • readme: update sample

Version 0.0.1

  • package: original commit