Skip to content

Commit

Permalink
Add code
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-ni committed Jan 23, 2021
0 parents commit 1146b03
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Files and directories created by pub
.dart_tool/
.packages

# Omit commiting pubspec.lock for library packages:
# https://dart.dev/guides/libraries/private-files#pubspeclock
pubspec.lock

# Conventional directory for build outputs
build/

# Directory created by dartdoc
doc/api/
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version, created by Stagehand
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Oliver Ni

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Bottom

This is a Dart implementation of [bottom](https://github.com/kaylynn234/bottom).

Bottom encodes UTF-8 text into a sequence comprised of bottom emoji (`πŸ«‚βœ¨πŸ₯Ίβ€οΈ`, with `,` sprinkled in for good measure) followed by `πŸ‘‰πŸ‘ˆ`. It can encode any valid UTF-8 β€” being a bottom transcends language, after all β€” and decode back into UTF-8.

## Usage

Here's a simple example:

```dart
import 'package:bottom/bottom.dart';
void main(List<String> args) {
print(bottom.encode(args.join(' ')));
}
```
14 changes: 14 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Defines a default set of lint rules enforced for
# projects at Google. For details and rationale,
# see https://github.com/dart-lang/pedantic#enabled-lints.
include: package:pedantic/analysis_options.yaml

# For lint rules and documentation, see http://dart-lang.github.io/linter/lints.
# Uncomment to specify additional rules.
# linter:
# rules:
# - camel_case_types

analyzer:
# exclude:
# - path/to/excluded/files/**
5 changes: 5 additions & 0 deletions example/bottom_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:bottom/bottom.dart';

void main(List<String> args) {
print(bottom.encode(args.join(' ')));
}
60 changes: 60 additions & 0 deletions lib/bottom.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
library bottom;

import 'dart:convert';

Map<String, int> _charValues = {
'πŸ«‚': 200,
'πŸ’–': 50,
'✨': 10,
'πŸ₯Ί': 5,
',': 1,
};

const _zero = '❀️';
const _sectionSeparator = 'πŸ‘‰πŸ‘ˆ';
final _terminator = RegExp('$_sectionSeparator?\$');

class BottomEncoder extends Converter<String, String> {
String _encodeByte(int value) {
if (value == 0) return '';
final largest =
_charValues.entries.firstWhere((entry) => value >= entry.value);
return largest.key + _encodeByte(value - largest.value);
}

String encodeByte(int value) {
return value == 0 ? _zero : _encodeByte(value);
}

@override
String convert(String input) {
return utf8
.encode(input)
.map((value) => encodeByte(value) + _sectionSeparator)
.join();
}
}

class BottomDecoder extends Converter<String, String> {
@override
String convert(String input) {
return utf8.decode(input
.trim()
.replaceAll(_terminator, '')
.split(_sectionSeparator)
.map((chars) => chars
.split('')
.map((x) => _charValues[x])
.reduce((a, b) => a + b)));
}
}

class BottomCodec extends Codec<String, String> {
@override
BottomEncoder get encoder => BottomEncoder();

@override
BottomDecoder get decoder => BottomDecoder();
}

final bottom = BottomCodec();
11 changes: 11 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: bottom
description: A starting point for Dart libraries or applications.
version: 1.0.0
homepage: https://github.com/oliver-ni/bottom-dart

environment:
sdk: ">=2.8.1 <3.0.0"

dev_dependencies:
pedantic: ^1.9.0
test: ^1.14.4
24 changes: 24 additions & 0 deletions test/bottom_dart_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:bottom/bottom.dart';
import 'package:test/test.dart';

Map<String, String> pairs = {
'Test': 'πŸ’–βœ¨βœ¨βœ¨,,,,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–βœ¨πŸ₯ΊπŸ‘‰πŸ‘ˆπŸ’–πŸ’–βœ¨πŸ₯Ί,πŸ‘‰πŸ‘ˆ',
'Hello World':
'πŸ’–βœ¨βœ¨,,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–πŸ₯Ί,,,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–πŸ₯Ί,,,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–βœ¨,πŸ‘‰πŸ‘ˆβœ¨βœ¨βœ¨,,πŸ‘‰πŸ‘ˆπŸ’–βœ¨βœ¨βœ¨πŸ₯Ί,,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–βœ¨,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–βœ¨,,,,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–πŸ₯Ί,,,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–πŸ‘‰πŸ‘ˆ',
'γŒγ‚“γ°γ‚Œ':
'πŸ«‚βœ¨βœ¨πŸ₯Ί,,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–βœ¨βœ¨πŸ₯Ί,,,,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–βœ¨βœ¨βœ¨βœ¨πŸ‘‰πŸ‘ˆπŸ«‚βœ¨βœ¨πŸ₯Ί,,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–βœ¨βœ¨βœ¨πŸ‘‰πŸ‘ˆπŸ’–πŸ’–βœ¨βœ¨βœ¨βœ¨πŸ₯Ί,,πŸ‘‰πŸ‘ˆπŸ«‚βœ¨βœ¨πŸ₯Ί,,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–βœ¨βœ¨πŸ₯Ί,,,,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–πŸ’–βœ¨βœ¨πŸ₯Ί,πŸ‘‰πŸ‘ˆπŸ«‚βœ¨βœ¨πŸ₯Ί,,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–βœ¨βœ¨βœ¨πŸ‘‰πŸ‘ˆπŸ’–πŸ’–βœ¨βœ¨βœ¨βœ¨πŸ‘‰πŸ‘ˆ',
'πŸ₯Ί': 'πŸ«‚βœ¨βœ¨βœ¨βœ¨πŸ‘‰πŸ‘ˆπŸ’–πŸ’–πŸ’–πŸ₯Ί,,,,πŸ‘‰πŸ‘ˆπŸ’–πŸ’–πŸ’–βœ¨πŸ₯ΊπŸ‘‰πŸ‘ˆπŸ’–πŸ’–πŸ’–βœ¨βœ¨βœ¨πŸ₯Ί,πŸ‘‰πŸ‘ˆ',
'\x00': 'β€οΈπŸ‘‰πŸ‘ˆ',
};

void main() {
group('Bottom tests', () {
test('Encodes properly', () {
pairs.forEach((text, result) => expect(bottom.encode(text), result));
});

test('Decodes properly', () {
pairs.forEach((text, result) => expect(bottom.decode(result), text));
});
});
}

0 comments on commit 1146b03

Please sign in to comment.