Skip to content

Commit

Permalink
Merge branch 'angus-c:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
EvandroLG authored Mar 19, 2022
2 parents 4c8a687 + 25f5955 commit 978f6e0
Show file tree
Hide file tree
Showing 16 changed files with 330 additions and 102 deletions.
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,34 +253,42 @@ Apply a diff object to an object. Pass converter to apply a http://jsonpatch.com
);
obj1; // {a: 4, c: 5}

const obj2 = {a: 3, b: 5};
diffApply(obj2,
[
{ "op": "move", "from": ['a'], "path": ['c']},
]
);
obj2; // {b: 5, c: 3}

// using converter to apply jsPatch standard paths
// see http://jsonpatch.com
import {diffApply, jsonPatchPathConverter} from 'just-diff-apply'
const obj2 = {a: 3, b: 5};
diffApply(obj2, [
const obj3 = {a: 3, b: 5};
diffApply(obj3, [
{ "op": "remove", "path": '/b' },
{ "op": "replace", "path": '/a', "value": 4 }
{ "op": "add", "path": '/c', "value": 5 }
], jsonPatchPathConverter);
obj2; // {a: 4, c: 5}
obj3; // {a: 4, c: 5}

// arrays (array key can be string or numeric)
const obj3 = {a: 4, b: [1, 2, 3]};
diffApply(obj3, [
const obj4 = {a: 4, b: [1, 2, 3]};
diffApply(obj4, [
{ "op": "replace", "path": ['a'], "value": 3 }
{ "op": "replace", "path": ['b', 2], "value": 4 }
{ "op": "add", "path": ['b', 3], "value": 9 }
]);
obj3; // {a: 3, b: [1, 2, 4, 9]}
obj4; // {a: 3, b: [1, 2, 4, 9]}

// nested paths
const obj4 = {a: 4, b: {c: 3}};
diffApply(obj4, [
const obj5 = {a: 4, b: {c: 3}};
diffApply(obj5, [
{ "op": "replace", "path": ['a'], "value": 5 }
{ "op": "remove", "path": ['b', 'c']}
{ "op": "add", "path": ['b', 'd'], "value": 4 }
]);
obj4; // {a: 5, b: {d: 4}}
obj5; // {a: 5, b: {d: 4}}
```

### [just-compare](https://www.npmjs.com/package/just-compare)
Expand Down
26 changes: 17 additions & 9 deletions md-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -579,34 +579,42 @@
" );",
" obj1; // {a: 4, c: 5}",
"",
" const obj2 = {a: 3, b: 5};",
" diffApply(obj2,",
" [",
" { \"op\": \"move\", \"from\": ['a'], \"path\": ['c']},",
" ]",
" );",
" obj2; // {b: 5, c: 3}",
"",
" // using converter to apply jsPatch standard paths",
" // see http://jsonpatch.com",
" import {diffApply, jsonPatchPathConverter} from 'just-diff-apply'",
" const obj2 = {a: 3, b: 5};",
" diffApply(obj2, [",
" const obj3 = {a: 3, b: 5};",
" diffApply(obj3, [",
" { \"op\": \"remove\", \"path\": '/b' },",
" { \"op\": \"replace\", \"path\": '/a', \"value\": 4 }",
" { \"op\": \"add\", \"path\": '/c', \"value\": 5 }",
" ], jsonPatchPathConverter);",
" obj2; // {a: 4, c: 5}",
" obj3; // {a: 4, c: 5}",
"",
" // arrays (array key can be string or numeric)",
" const obj3 = {a: 4, b: [1, 2, 3]};",
" diffApply(obj3, [",
" const obj4 = {a: 4, b: [1, 2, 3]};",
" diffApply(obj4, [",
" { \"op\": \"replace\", \"path\": ['a'], \"value\": 3 }",
" { \"op\": \"replace\", \"path\": ['b', 2], \"value\": 4 }",
" { \"op\": \"add\", \"path\": ['b', 3], \"value\": 9 }",
" ]);",
" obj3; // {a: 3, b: [1, 2, 4, 9]}",
" obj4; // {a: 3, b: [1, 2, 4, 9]}",
"",
" // nested paths",
" const obj4 = {a: 4, b: {c: 3}};",
" diffApply(obj4, [",
" const obj5 = {a: 4, b: {c: 3}};",
" diffApply(obj5, [",
" { \"op\": \"replace\", \"path\": ['a'], \"value\": 5 }",
" { \"op\": \"remove\", \"path\": ['b', 'c']}",
" { \"op\": \"add\", \"path\": ['b', 'd'], \"value\": 4 }",
" ]);",
" obj4; // {a: 5, b: {d: 4}}"
" obj5; // {a: 5, b: {d: 4}}"
]
},
"just-flush": {
Expand Down
13 changes: 13 additions & 0 deletions packages/array-intersect/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# just-intersect

## 4.1.1

### Patch Changes

- Update .mjs module and d.ts

## 4.1.0

### Minor Changes

- Performance improvement
29 changes: 24 additions & 5 deletions packages/array-intersect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,32 @@ function intersect(arr1, arr2) {
if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
throw new Error('expected both arguments to be arrays');
}

var result = [];
var len = arr1.length;
for (var i = 0; i < len; i++) {
var elem = arr1[i];
if (arr2.indexOf(elem) > -1 && result.indexOf(elem) == -1) {
result.push(elem);
var set = convertArrayToSet(arr2);
var memo = {};

for (var i = 0; i < arr1.length; i++) {
var item = arr1[i];

if (set.hasOwnProperty(item) && !memo.hasOwnProperty(item)) {
result.push(item);
memo[item] = true;
}
}

return result;
}

function convertArrayToSet(arr) {
var output = {};

for (var i = 0; i < arr.length; i++) {
var item = arr[i];
if (!output.hasOwnProperty(item)) {
output[item] = true;
}
}

return output;
}
29 changes: 24 additions & 5 deletions packages/array-intersect/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,34 @@ function intersect(arr1, arr2) {
if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
throw new Error('expected both arguments to be arrays');
}

var result = [];
var len = arr1.length;
for (var i = 0; i < len; i++) {
var elem = arr1[i];
if (arr2.indexOf(elem) > -1 && result.indexOf(elem) == -1) {
result.push(elem);
var set = convertArrayToSet(arr2);
var memo = {};

for (var i = 0; i < arr1.length; i++) {
var item = arr1[i];

if (set.hasOwnProperty(item) && !memo.hasOwnProperty(item)) {
result.push(item);
memo[item] = true;
}
}

return result;
}

function convertArrayToSet(arr) {
var output = {};

for (var i = 0; i < arr.length; i++) {
var item = arr[i];
if (!output.hasOwnProperty(item)) {
output[item] = true;
}
}

return output;
}

export {arrayIntersect as default};
2 changes: 1 addition & 1 deletion packages/array-intersect/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "just-intersect",
"version": "4.0.2",
"version": "4.1.1",
"description": "return the intersect of two arrays",
"main": "index.js",
"module": "index.mjs",
Expand Down
7 changes: 7 additions & 0 deletions packages/array-zip/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# just-zip-it

## 3.0.3

### Patch Changes

- Update .mjs module and d.ts

## 3.0.2

### Patch Changes

- Perf improvement
27 changes: 16 additions & 11 deletions packages/array-zip/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ var arrayZip = zip;
*/

function zip() {
var result = [];
var args = Array.prototype.slice.call(arguments);
var argsLen = args.length;
var maxLen = 0;
var i, j;

if (!argsLen) {
throw new Error('zip requires at least one argument');
}

for (i = 0; i < argsLen; i++) {
for (var i = 0; i < argsLen; i++) {
if (!Array.isArray(args[i])) {
throw new Error('all arguments must be arrays');
}
Expand All @@ -33,15 +31,22 @@ function zip() {
}
}

for (i = 0; i < maxLen; i++) {
var group = [];
for (j = 0; j < argsLen; j++) {
if (!Array.isArray(args[j])) {
throw new Error('all arguments must be arrays');
}
group[j] = args[j][i];
var result = [];
var group = [];
var p1 = 0;
var p2 = 0;

while (p1 < maxLen) {
group[p2] = args[p2][p1];

if (p2 < argsLen - 1) {
p2++;
} else {
p2 = 0;
result[p1] = group;
group = [];
p1++;
}
result[i] = group;
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion packages/array-zip/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "just-zip-it",
"version": "3.0.2",
"version": "3.0.3",
"description": "returns an array of grouped elements, taking n-th element from every given array",
"main": "index.js",
"module": "index.mjs",
Expand Down
13 changes: 13 additions & 0 deletions packages/collection-diff-apply/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# just-diff-apply

## 5.3.0

### Minor Changes

- Update .mjs module and d.ts

## 5.2.0

### Minor Changes

- Add support for 'move' op (https://datatracker.ietf.org/doc/html/rfc6902#section-4.4)

## 5.0.0

### Major Changes

- Disallow prototype updates
26 changes: 17 additions & 9 deletions packages/collection-diff-apply/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,40 @@ Apply a diff object to an object. Pass converter to apply a http://jsonpatch.com
);
obj1; // {a: 4, c: 5}

const obj2 = {a: 3, b: 5};
diffApply(obj2,
[
{ "op": "move", "from": ['a'], "path": ['c']},
]
);
obj2; // {b: 5, c: 3}

// using converter to apply jsPatch standard paths
// see http://jsonpatch.com
import {diffApply, jsonPatchPathConverter} from 'just-diff-apply'
const obj2 = {a: 3, b: 5};
diffApply(obj2, [
const obj3 = {a: 3, b: 5};
diffApply(obj3, [
{ "op": "remove", "path": '/b' },
{ "op": "replace", "path": '/a', "value": 4 }
{ "op": "add", "path": '/c', "value": 5 }
], jsonPatchPathConverter);
obj2; // {a: 4, c: 5}
obj3; // {a: 4, c: 5}

// arrays (array key can be string or numeric)
const obj3 = {a: 4, b: [1, 2, 3]};
diffApply(obj3, [
const obj4 = {a: 4, b: [1, 2, 3]};
diffApply(obj4, [
{ "op": "replace", "path": ['a'], "value": 3 }
{ "op": "replace", "path": ['b', 2], "value": 4 }
{ "op": "add", "path": ['b', 3], "value": 9 }
]);
obj3; // {a: 3, b: [1, 2, 4, 9]}
obj4; // {a: 3, b: [1, 2, 4, 9]}

// nested paths
const obj4 = {a: 4, b: {c: 3}};
diffApply(obj4, [
const obj5 = {a: 4, b: {c: 3}};
diffApply(obj5, [
{ "op": "replace", "path": ['a'], "value": 5 }
{ "op": "remove", "path": ['b', 'c']}
{ "op": "add", "path": ['b', 'd'], "value": 4 }
]);
obj4; // {a: 5, b: {d: 4}}
obj5; // {a: 5, b: {d: 4}}
```
2 changes: 1 addition & 1 deletion packages/collection-diff-apply/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Definitions by: Eddie Atkinson <https://github.com/eddie-atkinson>

type Operation = "add" | "replace" | "remove";
type Operation = "add" | "replace" | "remove" | "move";

type DiffOps = Array<{
op: Operation;
Expand Down
Loading

0 comments on commit 978f6e0

Please sign in to comment.