Skip to content

Commit 1da0fa0

Browse files
jonasfjlrhn
authored andcommitted
Added IdentityCodec (dart-archive/convert#17)
Added IdentityCodec
1 parent fd09f82 commit 1da0fa0

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

pkgs/convert/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.1.0
2+
3+
* Added an `IdentityCodec<T>` which implements `Codec<T,T>` for use as default
4+
value for in functions accepting an optional `Codec` as parameter.
5+
16
## 2.0.2
27

38
* Set max SDK version to `<3.0.0`, and adjust other dependencies.

pkgs/convert/lib/convert.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ library convert;
77
export 'src/accumulator_sink.dart';
88
export 'src/byte_accumulator_sink.dart';
99
export 'src/hex.dart';
10+
export 'src/identity_codec.dart';
1011
export 'src/percent.dart';
1112
export 'src/string_accumulator_sink.dart';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'dart:convert';
2+
3+
class _IdentityConverter<T> extends Converter<T, T> {
4+
const _IdentityConverter();
5+
T convert(T input) => input;
6+
}
7+
8+
/// A [Codec] that performs the identity conversion (changing nothing) in both
9+
/// directions.
10+
///
11+
/// The identity codec passes input directly to output in both directions.
12+
/// This class can be used as a base when combining multiple codecs,
13+
/// because fusing the identity codec with any other codec gives the other
14+
/// codec back.
15+
///
16+
/// Note, that when fused with another [Codec] the identity codec disppears.
17+
class IdentityCodec<T> extends Codec<T, T> {
18+
const IdentityCodec();
19+
20+
Converter<T, T> get decoder => _IdentityConverter<T>();
21+
Converter<T, T> get encoder => _IdentityConverter<T>();
22+
23+
/// Fuse with an other codec.
24+
///
25+
/// Fusing with the identify converter is a no-op, so this always return
26+
/// [other].
27+
Codec<T, R> fuse<R>(Codec<T, R> other) => other;
28+
}

pkgs/convert/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: convert
2-
version: 2.0.2
2+
version: 2.1.0
33
description: Utilities for converting between data representations.
44
author: Dart Team <[email protected]>
55
homepage: https://github.com/dart-lang/convert
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import 'dart:convert';
2+
import 'package:convert/convert.dart';
3+
import 'package:test/test.dart';
4+
5+
void main() {
6+
group('IdentityCodec', () {
7+
test('encode', () {
8+
final codec = IdentityCodec<String>();
9+
expect(codec.encode('hello-world'), equals('hello-world'));
10+
});
11+
12+
test('decode', () {
13+
final codec = IdentityCodec<String>();
14+
expect(codec.decode('hello-world'), equals('hello-world'));
15+
});
16+
17+
test('fuse', () {
18+
final stringCodec = IdentityCodec<String>();
19+
final utf8Strings = stringCodec.fuse(utf8);
20+
expect(utf8Strings, equals(utf8));
21+
});
22+
});
23+
}

0 commit comments

Comments
 (0)