Skip to content

Commit

Permalink
Rename sane to sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
Pante committed Aug 3, 2020
1 parent f8a7b78 commit 4eb9721
Show file tree
Hide file tree
Showing 22 changed files with 569 additions and 1 deletion.
2 changes: 1 addition & 1 deletion flint/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## 1.1.1 - Maybe one day I'll remember to update the README.md (27/07/20)
This update updates the outdated readme for the latest update
This update updates the outdated readme for the latest update.

## 1.1.0 - Flint City (26/07/2020)
This update focuses on improving the update tool and removing versioned `analysis_options` files.
Expand Down
14 changes: 14 additions & 0 deletions sugar/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# IntelliJ files
.idea/

# Files and directories created by pub
.dart_tool/
.packages
# Remove the following pattern if you wish to check in your lock file
pubspec.lock

# Conventional directory for build outputs
build/

# Directory created by dartdoc
doc/api/
14 changes: 14 additions & 0 deletions sugar/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: dart
sudo: required
dist: bionic

dart:
- stable

script:
- dartanalyzer --fatal-warnings lib
- pub run test test --platform vm
- pub run test_coverage

after_success:
- bash <(curl -s https://codecov.io/bash)
10 changes: 10 additions & 0 deletions sugar/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## 1.0.1 - Fix me up, Scotty

- Fix `GlobalDateTime.now()` returning the incorrect date and time.


## 1.0.0 - Initial Launch! 🚀

- Add collection library
- Add core library
- Add time library
21 changes: 21 additions & 0 deletions sugar/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 - 2020 Forus Labs

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.
31 changes: 31 additions & 0 deletions sugar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Sane: Forus Labs' Core Dart Library
[![Travis CI](https://img.shields.io/travis/forus-labs/sane/master?logo=travis)](https://travis-ci.com/forus-labs/sane)
[![Codecov](https://codecov.io/gh/forus-labs/sane/branch/master/graph/badge.svg)](https://codecov.io/gh/forus-labs/sane)
[![Pub Dev](https://img.shields.io/pub/v/sane)](https://pub.dev/packages/sane)

Sane is Forus Labs' core library that includes annotations, collection types, date-times and utilities for numerics and strings.

**This is a bleeding edge version of Sane. For a production ready and documented version, please refer to the [stable branch](https://github.com/forus-labs/sane/tree/stable).**


### [`sane.collection`](https://pub.dev/documentation/sane/latest/sane.collection/sane.collection-library.html)

* Delegating collection types
* Observable collection types
* Tuples (`Pair`, `Triple` and `Quad`)
* Pseudo-weak collection types

### [`sane.core`](https://pub.dev/documentation/sane/latest/sane.core/sane.core-library.html)

* Annotations (i.e. `nullable`, `tainted`)
* Pseudo-weak reference
* Utilities for numerics and strings

### [`sane.time`](https://pub.dev/documentation/sane/latest/sane.time/sane.time-library.html)

* Distinct global and localized date-time types
* `Period` type
* `Span` type
* `Time` type
* Utilities for rounding date-times

1 change: 1 addition & 0 deletions sugar/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:flint/analysis_options.dart.dev.yaml
1 change: 1 addition & 0 deletions sugar/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**Please refer to the [wiki](https://github.com/forus-labs/sane/wiki).**
9 changes: 9 additions & 0 deletions sugar/example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: sane_example
description: Example for sane
private: true
version: 1.0.0
homepage: https://github.com/forus-labs/cauldron
repository: https://github.com/forus-labs/cauldron

environment:
sdk: '>=2.8.4 <3.0.0'
4 changes: 4 additions & 0 deletions sugar/lib/collection.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library sugar.collection;

export 'src/collection/equality.dart';
export 'src/collection/fluent_iterable.dart';
5 changes: 5 additions & 0 deletions sugar/lib/core.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library sugar.core;

export 'src/core/equality.dart';
export 'src/core/math.dart';
export 'src/core/types.dart';
49 changes: 49 additions & 0 deletions sugar/lib/src/collection/equality.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
extension ListEquality<T> on List<T> {

bool equals(List<T> other) {
if (identical(this, other)) {
return true;
}

if (length != other.length) {
return false;
}

for (var i = 0; i < length; i++) {
if ([i] != other[i]) {
return false;
}
}

return true;
}

}

extension MapEquality<K, V> on Map<K, V> {

bool equals(Map<K, V> other) {
if (identical(this, other)) {
return true;
}

if (length != other.length) {
return false;
}

for (final entry in entries) {
if (entry.value != other[entry.key]) {
return false;
}
}

return true;
}

}

extension SetEquality<T> on Set<T> {

bool equals(Set<T> other) => identical(this, other) || (length == other.length && containsAll(other));

}
51 changes: 51 additions & 0 deletions sugar/lib/src/collection/fluent_iterable.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
extension FluentIterable<T> on Iterable<T> {

Iterable<T> distinct() sync* {
for (final element in toSet()) {
yield element;
}
}

Iterable<T> listen(void Function(T) listen) sync* {
for (final element in this) {
listen(element);
yield element;
}
}

Iterable<T> mapWhere(bool Function(T) predicate, T Function(T) replace) sync* {
for (final element in this) {
if (predicate(element)) {
yield replace(element);

} else {
yield element;
}
}
}


Iterable<MapEntry<int, T>> indexed({int initial = 0}) sync* {
for (final element in this) {
yield MapEntry(initial++, element);
}
}


bool none(bool Function(T) predicate) {
for (final element in this) {
if (predicate(element)) {
return false;
}
}

return true;
}


Map<K, V> toMap<K, V>(K Function(T) key, V Function(T) value) => {
for (final element in this)
key(element) : value(element)
};

}
99 changes: 99 additions & 0 deletions sugar/lib/src/collection/tuple.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import 'package:meta/meta.dart';

import 'package:sugar/core.dart';

typedef Apply<T, R> = R Function(T value);


@immutable class Pair<K, V> with Equality implements MapEntry<K, V> {

@override final K key;
@override final V value;
@override final List<dynamic> fields;

Pair(this.key, this.value): fields = List.unmodifiable([key, value]);


Pair<K, V> replace({K key, V value}) => Pair(key ?? this.key, value ?? this.value);

Pair<K1, V1> map<K1, V1>(Apply<K, K1> key, Apply<V, V1> value) => Pair(key(this.key), value(this.value));

T reduce<T>(T Function(K, V) reduce) => reduce(key, value);

}

@immutable class Triple<L, M, R> with Equality {

final L left;
final M middle;
final R right;
@override final List<dynamic> fields;

Triple(this.left, this.middle, this.right): fields = List.unmodifiable([left, middle, right]);


Triple<L, M, R> replace({L left, M middle, R right}) => Triple(left ?? this.left, middle ?? this.middle, right ?? this.right);

Triple<L1, M1, R1> map<L1, M1, R1>(Apply<L, L1> left, Apply<M, M1> middle, Apply<R, R1> right) => Triple(left(this.left), middle(this.middle), right(this.right));

T reduce<T>(T Function(L, M, R) reduce) => reduce(left, middle, right);

}

@immutable class Quad<T1, T2, T3, T4> with Equality {

final T1 first;
final T2 second;
final T3 third;
final T4 fourth;
@override final List<dynamic> fields;

Quad(this.first, this.second, this.third, this.fourth): fields = List.unmodifiable([first, second, third, fourth]);


Quad<T1, T2, T3, T4> replace({T1 first, T2 second, T3 third, T4 fourth}) => Quad(first ?? this.first, second ?? this.second, third ?? this.third, fourth ?? fourth);

Quad<R1, R2, R3, R4> map<R1, R2, R3, R4>(Apply<T1, R1> first, Apply<T2, R2> second, Apply<T3, R3> third, Apply<T4, R4> fourth)
=> Quad(first(this.first), second(this.second), third(this.third), fourth(this.fourth));

T reduce<T>(T Function(T1, T2, T3, T4) reduce) => reduce(first, second, third, fourth);

}


extension TupleIterable on Iterable<Iterable<dynamic>> {

Iterable<Pair<K, V>> pairs<K, V>() sync* {
for (final iterable in this) {
if (iterable.length == 2) {
yield Pair<K, V>(iterable.elementAt(0), iterable.elementAt(1));

} else {
throw ArgumentError.value(iterable, 'Pair has a length of: ${iterable.length}, should be 2');
}
}
}

Iterable<Triple<L, M, R>> triples<L, M, R>() sync* {
for (final iterable in this) {
if (iterable.length == 3) {
yield Triple<L, M, R>(iterable.elementAt(0), iterable.elementAt(1), iterable.elementAt(2));

} else {
throw ArgumentError.value(iterable, 'Triple has a length of: ${iterable.length}, should be 3');
}
}
}

Iterable<Quad<T1, T2, T3, T4>> quads<T1, T2, T3, T4>() sync* {
for (final iterable in this) {
if (iterable.length == 4) {
yield Quad<T1, T2, T3, T4>(iterable.elementAt(0), iterable.elementAt(1), iterable.elementAt(2), iterable.elementAt(4));

} else {
throw ArgumentError.value(iterable, 'Quad has a length of: ${iterable.length}, should be 4');
}
}
}

}
20 changes: 20 additions & 0 deletions sugar/lib/src/core/equality.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:meta/meta.dart';

import 'package:sugar/collection.dart';
import 'package:sugar/core.dart';

mixin Equality {

@override
bool operator == (dynamic other) => identical(this, other) || (other.subclassOf(this) && fields.equals(other.fields));

@override
int get hashCode => hash(fields);

@override
String toString() => '$runtimeType(${fields.join(', ')})';


@protected List<dynamic> get fields;

}
Loading

0 comments on commit 4eb9721

Please sign in to comment.