basic_utils 2.3.8

  • Readme
  • Changelog
  • Example
  • Installing
  • 96

Basic Utils #

A dart package for many helper methods fitting different situations.

Table of Contents #

  1. Preamble
  2. Install
  3. Import
  4. Util Classes
  5. Changelog
  6. Copyright and license

Preamble #

This package is also part of the EZ Flutter Framework.

Install #

pubspec.yaml #

Update pubspec.yaml and add the following line to your dependencies.

dependencies:
  basic_utils: ^2.3.8

Import #

Import the package with :

import 'package:basic_utils/basic_utils.dart';

Util Classes #

The package contains different classes. Each class contains methods that provide a solution for certain problems.

StringUtils #

Helper class for String operations.

String defaultString(String str, {String defaultStr = ''});
bool isNullOrEmpty(String s);
bool isNotNullOrEmpty(String s);
String camelCaseToUpperUnderscore(String s);
String camelCaseToLowerUnderscore(String s);
bool isLowerCase(String s);
bool isUpperCase(String s);
bool isAscii(String s);
String capitalize(String s);
String reverse(String s);
int countChars(String s, String char, {bool caseSensitive = true});
bool isDigit(String s);
bool equalsIgnoreCase(String a, String b);
bool inList(String s, List<String> list, {bool ignoreCase = false});
bool isPalindrome(String s);
String hidePartial(String s, {int begin = 0, int end, String replace = "*"});
String addCharAtPosition(String s, String char, int position,{bool repeat = false});
List<String> chunk(String s, chunkSize);

DomainUtils #

Helper class for operations on domain names.

bool isDomainName(String s);
bool isSubTld(String tld, String subTld);
bool isSubDomain(String s);
bool isSubDomainOf(String sub, String domain);
bool isCCTLD(String s);
bool isNGTLD(String s);
bool isTld(String s);
bool isGTLD(String s);
List<String> splitDomainName(String domainName);
Domain getDomainFromUrl(String url);
Domain parseDomain(String domainName);
List<String> splitSubdomainInDomains(String name);

EmailUtils #

Helper class for operations on email addresses.

bool isEmail(String s);
EmailAddress parseEmailAddress(String s);

MathUtils #

Helper class for simple math operations like calculating circular area or converting length units.

double calculateCircumference(double radius);
double calculateCircularArea(double radius);
double calculateCircleDiameter(double radius);
double calculateSquareArea(double a, {double b});
double convertUnit(double value, LengthUnits sourceUnit, LengthUnits targetUnit);
double calculateMixingTemperature(double mA, double tA, double mB, double tB,{double cA, double cB});
num mean(List<num> l);
double round(double value, int decimals);

HttpUtils #

Helper class for simple http operations like sending requests.

Future<Map<Response> getForFullResponse(String url, {Map<String, dynamic> queryParameters, Map<String, String> headers});
Future<Map<String, dynamic>> getForJson(String url, {Map<String, dynamic> queryParameters, Map<String, String> headers});
Future<String> getForString(String url, {Map<String, dynamic> queryParameters, Map<String, String> headers});
Future<Map<Response> postForFullResponse(String url, String body, {Map<String, String> queryParameters, Map<String, String> headers});
Future<Map<String, dynamic>> postForJson(String url, String body, {Map<String, String> queryParameters, Map<String, String> headers});
Future<String> postForString(String url, String body, {Map<String, String> queryParameters, Map<String, String> headers});
Future<Response> putForFullResponse(String url, String body, {Map<String, String> queryParameters, Map<String, String> headers});
Future<Map<String, dynamic>> putForJson(String url, String body, {Map<String, String> queryParameters, Map<String, String> headers});
Future<String> putForString(String url, String body, {Map<String, String> queryParameters, Map<String, String> headers});
Future<Response deleteForFullResponse(String url, {Map<String, String> queryParameters, Map<String, String> headers});
Future<Map<String, dynamic>> deleteForJson(String url, {Map<String, String> queryParameters, Map<String, String> headers});
Future<String> deleteForString(String url, {Map<String, String> queryParameters, Map<String, String> headers});
Map<String, dynamic> getQueryParameterFromUrl(String url);
String addQueryParameterToUrl(String url, Map<String, dynamic> queryParameters);

DnsUtils #

Helper class for lookup resource records. Uses google dns resolver api.

Future<List<RRecord>> lookupRecord(String name, RRecordType type,{bool dnssec = false});

SortUtils #

Helper class for sorting lists. Implementation of different sorting algorithms.

List quickSort(List list);
List bubbleSort(List list);
List heapSort(List list);

ColorUtils #

Helper class for color operations.

int hexToInt(String hex);
String intToHex(int i);
String shadeColor(String hex, int percent);
String fillUpHex(String hex);
bool isDark(String hex);
String contrastColor(String hex);
Map<String, int> basicColorsFromHex(String hex);
double calculateRelativeLuminance(int red, int green, int blue,{int decimals = 2});
List<String> swatchColor(String hex, {double percentage = 15, int amount = 5});

DateUtils #

Helper class for date operations like converting textual datetime description.

DateTime stringToDateTime(String s, {DateTime time});

X509Utils #

Helper class for operations on x509 certificates, like generating key pairs, generating csr and many more.

AsymmetricKeyPair generateKeyPair({int keySize = 2048});
String formatKeyString(String key, String begin, String end,{int chunkSize = 64, String lineDelimiter = "\n"});
String generateRsaCsrPem(Map<String, String> attributes,RSAPrivateKey privateKey, RSAPublicKey publicKey);
String encodeASN1ObjectToPem(ASN1Object asn1Object, String begin, String end);
String encodeRSAPublicKeyToPem(RSAPublicKey publicKey);
String encodeRSAPrivateKeyToPem(RSAPrivateKey rsaPrivateKey);
RSAPrivateKey privateKeyFromPem(String pem);
RSAPublicKey publicKeyFromPem(String pem);
Uint8List getBytesFromPEMString(String pem);
RSAPrivateKey privateKeyFromDERBytes(Uint8List bytes);
RSAPrivateKey privateKeyFromASN1Sequence(ASN1Sequence asnSequence);
Uint8List rsaPublicKeyModulusToBytes(RSAPublicKey publicKey);
Uint8List rsaPublicKeyExponentToBytes(RSAPublicKey publicKey);
Uint8List rsaPrivateKeyToBytes(RSAPrivateKey privateKey);
ASN1Object encodeDN(Map<String, String> dn);
X509CertificateData x509CertificateFromPem(String pem);

IterableUtils #

Helper class for operations on iterables

T randomItem<T>(Iterable<T> iterable);

CryptoUtils #

Helper class for cryptographic operations.

String getSha1ThumbprintFromBytes(Uint8List bytes);
String getMd5ThumbprintFromBytes(Uint8List bytes);

Changelog #

For a detailed changelog, see the CHANGELOG.md file

MIT License

Copyright (c) 2019 Ephenodrom

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.

Changelog #

[2.3.8] - 2019-11-29

  • Improve X509Utils (#8)
  • Improve UnitTests

[2.3.7] - 2019-11-25

  • Improve DomainUtils (#7)
  • Improve UnitTests

[2.3.6] - 2019-11-25

  • Improve DomainUtils (#6)
  • Improve UnitTests

[2.3.5] - 2019-11-25

  • Improve StringUtils (#5)
  • Improve DomainUtils (#6)
  • Improve UnitTests

[2.3.4] - 2019-11-21

  • Fix health suggestions

[2.3.3] - 2019-11-12

  • Add Json Serialization To X509CertificateData Model

[2.3.2] - 2019-11-11

  • Improve X509CertificateData Model
  • Improve X509Utils

[2.3.1] - 2019-11-08

  • Update pubspec.yaml

[2.3.0] - 2019-11-06

  • Added CryptoUtils
  • Improve X509Utils
  • Improve X509CertificateData Model
  • Improve and add Unit tests

[2.2.5] - 2019-11-04

  • Improve X509Utils
  • Improve X509CertificateData Model
  • Improve and add Unit tests

[2.2.4] - 2019-10-21

  • Export models

[2.2.3] - 2019-10-21

  • Improve X509Utils

[2.2.2] - 2019-10-21

  • Improve X509Utils
  • Add Unit test

[2.2.1] - 2019-10-18

  • Minor fix

[2.2.0] - 2019-10-18

  • Add IterableUtils (Pull Request)
  • Add Unit test

[2.1.0] - 2019-10-16

  • Improve StringUtils
  • Add X509Utils
  • Add Unit test

[2.0.3] - 2019-10-14

  • Improve DomainUtils
  • Add Unit test

[2.0.2] - 2019-09-23

  • Improve ColorUtils
  • Add Unit test

[2.0.1] - 2019-09-12

  • Improve ColorUtils
  • Improve MathUtils
  • Add Unit test

[2.0.0] - 2019-09-04

  • Improve ColorUtils
  • Add Unit test

[1.9.3] - 2019-09-03

  • Improve documentation of DateUtils

[1.9.2] - 2019-09-03

  • Fix minor bug

[1.9.1] - 2019-09-02

  • Fix minor bug

[1.9.0] - 2019-09-02

  • Add DateUtils
  • Add unit tests

[1.8.2] - 2019-08-30

  • Fix minor bug
  • Update dependecies

[1.8.1] - 2019-08-28

  • Fix minor bug

[1.8.0] - 2019-08-28

  • Add new ColorUtils
  • Add new unit tests

[1.7.0] - 2019-08-26

  • Add ColorUtils
  • Add unit tests

[1.6.0] - 2019-08-08

  • Switch from import files to export files
  • Improve error handling in HttpUtils

[1.5.1] - 2019-08-05

  • Fix "Health suggestions" from pub.dev

[1.5.0] - 2019-07-17

  • Improve StringUtils

[1.4.0] - 2019-07-15

  • Improve StringUtils

[1.3.0] - 2019-07-08

  • Improve HttpUtils

[1.2.0] - 2019-06-14

  • Add SortUtils
  • Improve MathUtils

[1.1.1] - 2019-05-06

  • Improve DnsUtils

[1.1.0] - 2019-05-06

  • Fix DnsUtils

[1.0.9] - 2019-05-06

  • Clear and improve code.

[1.0.8] - 2019-05-02

  • Improve HttpUtils to handle simple string responses.

[1.0.7] - 2019-04-28

  • Added new StringUtils method
  • Added new unit tests

[1.0.6] - 2019-04-26

  • Added new DnsUtils
  • Added new unit tests

[1.0.5] - 2019-04-16

  • Added new HttpUtils
  • Added new unit tests

[1.0.4] - 2019-04-10

  • Added new StringUtils
  • Added new unit tests
  • Improve documentation

[1.0.3] - 2019-04-02

  • Added new MathUtils
  • Added new EmailUtils
  • Added new unit tests

[1.0.2] - 2019-03-06

  • Added new helper functions
  • Added new unit tests

[1.0.1] - 2019-02-21

  • Minor changes

[1.0.0] - 2019-02-21

  • Initial release

example/main.dart

import 'package:basic_utils/basic_utils.dart';

void main() async {
  // StringUtils
  print("Convert to lowercase underscore: " +
      StringUtils.camelCaseToLowerUnderscore("camelCase"));
  print("Is lowercase: " + StringUtils.isLowerCase("lowercase").toString());
  print("Is uppercase: " + StringUtils.isUpperCase("UPPERCASE").toString());
  print("Is ascii: " + StringUtils.isAscii("Hello").toString());
  print(
      "Is null or empty: " + StringUtils.isNullOrEmpty("notempty").toString());

  // DomainUtils
  print("Is dartlang.org a domain name? " +
      DomainUtils.isDomainName("dartlang.org").toString());
  Domain domain = DomainUtils.parseDomain("dartlang.org");
  print("Sld = " + domain.sld + " & tld = " + domain.tld);

  // EmailUtils
  print("Is hello@world.com an email ? " +
      EmailUtils.isEmail("hello@world.com").toString());

  // MathUtils
  print("1 km is " +
      MathUtils.convertUnit(1, LengthUnits.kilometer, LengthUnits.meter)
          .toString() +
      " meter.");
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  basic_utils: ^2.3.8

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:basic_utils/basic_utils.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
93
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
96
Learn more about scoring.

We analyzed this package on Dec 12, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.1+4

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
asn1lib ^0.5.14 0.5.14
convert ^2.1.1 2.1.1
crypto ^2.1.3 2.1.4
http ^0.12.0 0.12.0+2
json_annotation >=2.0.0 <=3.0.0 3.0.0
logging ^0.11.3+2 0.11.3+2
pointycastle ^1.0.1 1.0.2
Transitive dependencies
async 2.4.0
charcode 1.1.2
collection 1.14.12
http_parser 3.1.3
meta 1.1.8
path 1.6.4
pedantic 1.9.0
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
build_runner ^1.5.0
json_serializable ^3.2.2
test >=0.12.42 <2.0.0