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

dart 3 update with some coding improvment #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:very_good_analysis/analysis_options.yaml

linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at
# https://dart-lang.github.io/linter/lints/index.html.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
public_member_api_docs: false
57 changes: 33 additions & 24 deletions lib/src/lipsum.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
/// A simple library for generating lorem ipsum text
library lipsum;

import "dart:math";
import 'words.dart';
import 'dart:math';
import 'package:lipsum/src/words.dart';

_randomInt(int min, int max) {
Random rnd = new Random();
int _randomInt(int min, int max) {
final rnd = Random();
return rnd.nextInt((max - min) + 1) + min;
}

/// Creates [numWords] number of random words.
String createWord({int numWords = 1}) {
Random _random;
Random random;

if (numWords > 1) {
return createSentence(sentenceLength: numWords, numSentences: 1);
return createSentence(sentenceLength: numWords);
}

_random = Random();
return words[_random.nextInt(words.length)];
random = Random();
return words[random.nextInt(words.length)];
}

/// Creates random sentences.
Expand All @@ -29,17 +29,21 @@ String createWord({int numWords = 1}) {
String createSentence({int sentenceLength = -1, int numSentences = 1}) {
int wordIndex;
String sentence;
var newSentenceLength = sentenceLength;

if (numSentences > 1)
if (newSentenceLength > 1) {
return createParagraph(numSentences: numSentences);
}

if (sentenceLength < 0) {
sentenceLength = _randomInt(5, 20);
newSentenceLength = _randomInt(5, 20);
}

wordIndex = _randomInt(0, words.length - sentenceLength - 1);
sentence = words.getRange(wordIndex, wordIndex + sentenceLength).join(" ");
sentence = sentence[0].toUpperCase() + sentence.substring(1) + ".";
wordIndex = _randomInt(0, words.length - newSentenceLength - 1);
sentence = words.getRange(wordIndex, wordIndex + newSentenceLength).join(' ');
if (sentenceLength > 0) {
sentence = '${sentence[0].toUpperCase()}${sentence.substring(1)}.';
}

return sentence;
}
Expand All @@ -50,19 +54,24 @@ String createSentence({int sentenceLength = -1, int numSentences = 1}) {
/// [numSentences] long. [numParagraphs] specifies the number of paragraphs
/// to generate.
String createParagraph({int numSentences = -1, int numParagraphs = 1}) {
List<String> sentences = [];

if (numParagraphs > 1)
return createText(numSentences: numSentences, numParagraphs: numParagraphs);
final sentences = <String>[];
var newNumSentences = numSentences;

if (numParagraphs > 1) {
return createText(
numSentences: newNumSentences,
numParagraphs: numParagraphs,
);
}

if (numSentences < 0) {
numSentences = _randomInt(3, 5);
newNumSentences = _randomInt(3, 5);
}
for (var i = 0; i < numSentences; i++) {
sentences.add(createSentence());
}

return sentences.getRange(0, sentences.length).join(" ");
return sentences.getRange(0, sentences.length).join(' ');
}

/// Creates a text comprised of a number of paragraphs.
Expand All @@ -71,14 +80,14 @@ String createParagraph({int numSentences = -1, int numParagraphs = 1}) {
/// contain [numSentences] sentences. If either parameter is omitted, a
/// random number is generated.
String createText({int numParagraphs = -1, int numSentences = -1}) {
List<String> paragraphs = [];

final paragraphs = <String>[];
var newNumParagraphs = numParagraphs;
if (numParagraphs < 0) {
numParagraphs = _randomInt(3, 7);
newNumParagraphs = _randomInt(3, 7);
}
for (var i = 0; i < numParagraphs; i++) {
for (var i = 0; i < newNumParagraphs; i++) {
paragraphs.add(createParagraph(numSentences: numSentences));
}

return paragraphs.getRange(0, paragraphs.length).join("\n");
return paragraphs.getRange(0, paragraphs.length).join('\n');
}
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors:
- Adaptant Labs <[email protected]>
homepage: https://github.com/adaptant-labs/lipsum-dart
environment:
sdk: '>=1.24.3 <3.0.0'
sdk: ">=3.0.0 <4.0.0"

dependencies:
very_good_analysis: ^5.0.0+1
dev_dependencies:
test: any