Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR] Adding Dart package #97

Merged
merged 7 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Dart CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: macos-latest

steps:
- uses: actions/checkout@v3

# Installing Flutter because it's easier to generate .lcov files for test coverage
- name: Install Flutter
uses: subosito/flutter-action@v2

- name: Install dependencies
run: dart pub get

# Your project will need to have tests in test/ and a dependency on
# package:test for this step to succeed. Note that Flutter projects will
# want to change this to 'flutter test'.
- name: Run tests
run: flutter test --coverage

- uses: codecov/codecov-action@v2
with:
files: coverage/lcov.info
verbose: true # optional (default = false)
File renamed without changes.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ package-lock.json
node_modules
coverage
.nyc_output

# Ignore Dart-related package files
pubspec.lock
.dart_tool/
build
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ several languages.

[![Hex pm](http://img.shields.io/hexpm/v/quotes.svg?style=flat-square)](https://hex.pm/packages/quotes)
[![npm package version](https://img.shields.io/npm/v/quotesy.svg?style=flat-square)](https://www.npmjs.com/package/quotesy)
[![Build Status](https://img.shields.io/travis/dwyl/quotes/master.svg?style=flat-square)](https://travis-ci.org/dwyl/quotes)
![Elixir Build Status](https://img.shields.io/github/actions/workflow/status/dwyl/quotes/elixir.yml?label=Elixir&style=flat-square)
![Dart Build Status](https://img.shields.io/github/actions/workflow/status/dwyl/quotes/dart.yml?label=Dart&style=flat-square)
[![codecov.io](https://img.shields.io/codecov/c/github/dwyl/quotes/main.svg?style=flat-square)](http://codecov.io/github/dwyl/quotes?branch=main)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat-square)](https://github.com/dwyl/quotes#contributing)
[![HitCount](http://hits.dwyl.com/dwyl/quotes.svg)](http://hits.dwyl.com/dwyl/quotes)
Expand Down Expand Up @@ -151,6 +152,70 @@ iex> Quotes.random_by_tag("curious")

<br />

## `Dart`

### Install from `Dart` packages

```sh
dart pub get quotesy
```

### `Dart` sample code

- Get the whole `list` of quotes.

```dart
import 'package:quotesy/quotesy.dart';

final quotesArray = await Quotes.list();
[
{
"author": "Peter Drucker",
"text": "The best way to predict your future is to create it."
}
]
```

- Get the list of quotes from a specific author.

```dart
import 'package:quotesy/quotesy.dart';

final authorQuotesArray = await Quotes.byAuthor("Peter Drucker");
[
{
"author": "Peter Drucker",
"text": "The best way to predict your future is to create it."
}
]
```

- Get a `random` quote.

```dart
import 'package:quotesy/quotesy.dart';

final randomQuote = await Quotes.random();
{
"author": "Zig Ziglar",
"text": "Positive thinking will let you do everything better than negative thinking will."
}
```

- Get a `random` quote from an author.

```dart
import 'package:quotesy/quotesy.dart';

final randomQuote = await Quotes.singleRandomByAuthor("William Shakespeare");
{
"author": "William Shakespeare",
"text": "Speak low, if you speak love."
}
```

<br />

## `JavaScript` / `Node.js`

### Install from NPM
Expand Down
30 changes: 30 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# Uncomment the following section to specify additional rules.

# linter:
# rules:
# - camel_case_types

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
64 changes: 64 additions & 0 deletions lib/quotesy.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
library quotesy;

import 'dart:convert';
import 'dart:io';
import 'dart:math';

/// Class holding a quote information
class QuoteInfo {
final String author;
final String text;
final String? source;
final String? tags;

QuoteInfo(this.text, this.author, {this.source, this.tags});

factory QuoteInfo.fromJson(Map<String, dynamic> json) {
return QuoteInfo(
json['text'],
json['author'],
source: json['source'],
tags: json['tags'],
);
}
}

/// Static class that can be invoked to fetch quotes
class Quotes {

/// Loads the information from the `.json` file containing the quotes list.
static Future<List<QuoteInfo>> _quotesList() async {
String jsonString = await File('quotes.json').readAsString();
List<dynamic> jsonList = json.decode(jsonString);
List<QuoteInfo> quotes = jsonList.map((json) => QuoteInfo.fromJson(json)).toList();
return quotes;
}

/// Returns a list of quotes
static list() async {
return await _quotesList();
}

/// Returns a random quote
static Future<QuoteInfo> random() async {
final random = Random();
final list = await _quotesList();

return list[random.nextInt(list.length)];
}

/// Returns a list of quotes by author
static Future<List<QuoteInfo>> byAuthor(String author) async {
final list = await _quotesList();
return list.where((quote) => quote.author == author).toList();
}

/// Returns a random quote by author
static Future<QuoteInfo> singleRandomByAuthor(String author) async {
final list = await _quotesList();
final listByAuthor = list.where((quote) => quote.author == author).toList();
final random = Random();

return listByAuthor[random.nextInt(listByAuthor.length)];
}
}
18 changes: 18 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: quotesy
description: A curated list of quotes.
version: 1.0.0
repository: https://github.com/dwyl/quotes

environment:
sdk: '>=2.19.2 <3.0.0'

# dependencies:
# path: ^1.8.0

dev_dependencies:
lints: ^2.0.0
test: ^1.21.0

flutter:
assets:
- ./quotes.json
27 changes: 27 additions & 0 deletions test/quotesy_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:quotesy/quotesy.dart';
import 'package:test/test.dart';

void main() {
group('A group of tests', () {
test('Fetching quotes list', () async {
final quotesArray = await Quotes.list();
expect(quotesArray.length, greaterThan(0));
expect(quotesArray.first.author, "Abraham Lincoln");
});

test('Fetching random element', () async {
final randomQuote = await Quotes.random();
expect(randomQuote, isNotNull);
});

test('Fetching by author', () async {
final quoteList = await Quotes.byAuthor("Abraham Lincoln");
expect(quoteList.length, greaterThan(0));
});

test('Fetching random quote by author', () async {
final quoteList = await Quotes.singleRandomByAuthor("Abraham Lincoln");
expect(quoteList.author, "Abraham Lincoln");
});
});
}