Skip to content

Commit 8dbdb26

Browse files
committed
working on dart package
1 parent 1e65eeb commit 8dbdb26

File tree

12 files changed

+182
-1
lines changed

12 files changed

+182
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/python3/build
33
/python3/dist
44
/python3/*.egg-info
5+
/dart/.dart_tool

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.1.5
2+
3+
- Initial version with support for TypeScript and Python3

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The `diff` and `patch` functions are implemented in the following languages:
1515

1616
* TypeScript
1717
* Python3
18+
* Dart (experimental)
1819

1920
The `diffData` object **can be used interchangeably between the languages**.\
2021
This makes this library very useful for networked applications, where the client and server are written in different languages.

dart/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/dart/.dart_tool

dart/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.1.0
2+
3+
- Initial version added to the repository

dart/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Fabio Rotondo
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

dart/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# diff-patch
2+
3+
## Introduction
4+
5+
diff-patch is a library that exposes two functions, `diff` and `patch`.
6+
7+
The library does not have any dependencies.
8+
9+
The `diff` function takes two objects (`obj1` and `obj2`) and deep compares them, returning an object that represents the difference between the two (the `diffData`).
10+
`diffData` is an object that can be used to apply the diff to `obj1` to get an object that is the same as `obj2`.
11+
12+
The `patch` function takes an `obj` and a `diffData` object and applies the diff to the object, returning a new object.
13+
14+
The `diff` and `patch` functions are implemented in the following languages:
15+
16+
* TypeScript
17+
* Python3
18+
* Dart (experimental)
19+
20+
The `diffData` object **can be used interchangeably between the languages**.\
21+
This makes this library very useful for networked applications, where the client and server are written in different languages.
22+
23+
`diffData` can be serialized to JSON and deserialized back to an object. It aims to be as small as possible, and is designed to be used in a networked environment.
24+
25+
More languages will be added in the future, contributions are welcome.
26+
27+
## Usage
28+
29+
### Dart
30+
31+
To install the Dart version of diff-patch, run:
32+
33+
```bash
34+
dart pub add fsoft_diff_patch
35+
```
36+
37+
Then, in your Dart code:
38+
39+
```dart
40+
import { diff, patch } from '@fsoft/diff-patch';
41+
42+
void main() {
43+
final a = {
44+
"a": 1,
45+
"b": 2,
46+
"c": {
47+
"d": 3,
48+
"e": 4,
49+
"f": {
50+
"g": 5,
51+
"h": 6,
52+
},
53+
},
54+
};
55+
final b = {
56+
"a": 1,
57+
// "b": 2, // b is deleted
58+
"c": {
59+
"d": 9, // d is changed
60+
"e": 4,
61+
"f": {
62+
"g": 5,
63+
"h": 7, // h is changed
64+
"i": 10, // i is added
65+
},
66+
},
67+
};
68+
// diff a and b and create a diffData object
69+
final diffData = diff(a, b);
70+
// print diffData in the console
71+
print("DIFF DATA\n${json.encode(diffData)}");
72+
// apply diffData to a and create a new object
73+
final patched_a = patch(a, diffData);
74+
// print patched_a in the console
75+
print("\n\nPATCHED OBJ\n${json.encode(patched_a)}");
76+
// check if patched_a is equal to b
77+
print("\n\nSAME OBJECT: ${json.encode(patched_a) == json.encode(b)}");
78+
}
79+
```
80+
81+
## Contributors
82+
83+
This library was created by [Fabio Rotondo](https://github.com/fsoft72).
84+
85+
New language implementations are more than welcome.\
86+
Please open an issue or a pull request if you want to contribute.
87+
88+
**Collaborators**:
89+
90+
* [Nikola Gluhovic](https://github.com/nini-os)
91+
92+
93+
The official repository for this library is [here](https://github.com/fsoft72/diff-patch).
94+
95+
## License
96+
97+
This library is licensed under the MIT License.\
98+
See the [LICENSE](LICENSE) file for details.

dart/analysis_options.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file configures the static analysis results for your project (errors,
2+
# warnings, and lints).
3+
#
4+
# This enables the 'recommended' set of lints from `package:lints`.
5+
# This set helps identify many issues that may lead to problems when running
6+
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7+
# style and format.
8+
#
9+
# If you want a smaller set of lints you can change this to specify
10+
# 'package:lints/core.yaml'. These are just the most critical lints
11+
# (the recommended set includes the core lints).
12+
# The core lints are also what is used by pub.dev for scoring packages.
13+
14+
include: package:lints/recommended.yaml
15+
16+
# Uncomment the following section to specify additional rules.
17+
18+
# linter:
19+
# rules:
20+
# - camel_case_types
21+
22+
# analyzer:
23+
# exclude:
24+
# - path/to/excluded/files/**
25+
26+
# For more information about the core and recommended set of lints, see
27+
# https://dart.dev/go/core-lints
28+
29+
# For additional information about configuring this file, see
30+
# https://dart.dev/guides/language/analysis-options
File renamed without changes.

dart/pubspec.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Generated by pub
2+
# See https://dart.dev/tools/pub/glossary#lockfile
3+
packages:
4+
lints:
5+
dependency: "direct dev"
6+
description:
7+
name: lints
8+
sha256: "5e4a9cd06d447758280a8ac2405101e0e2094d2a1dbdd3756aec3fe7775ba593"
9+
url: "https://pub.dev"
10+
source: hosted
11+
version: "2.0.1"
12+
sdks:
13+
dart: ">=2.18.0 <3.0.0"

dart/pubspec.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: fsoft_diff_patch
2+
description: Deep compare two objects and produces a diffData to transform object1 to object2
3+
version: 0.1.0
4+
repository: https://github.com/fsoft72/diff-patch
5+
6+
environment:
7+
sdk: '>=2.18.0 <3.0.0'
8+
9+
dev_dependencies:
10+
lints: ^2.0.0

tests/dart/test01.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'dart:convert';
22

3-
import '../../dart/diff-patch.dart';
3+
import '../../dart/lib/fsoft_diff_patch.dart';
44

55
void main() {
66
final a = {

0 commit comments

Comments
 (0)