safe 0.1.8

safe #

安全的 set/get 对象操作

API #

API Documentation

Getting Started #

这是一个基础的用例:

import 'package:safe/safe.dart' as safe;

void main() {
  final dynamic v = {
    'dog': [
      'aa',
      'bb',
      10,
      {'sub': '123'},
      [10, 20]
    ],
    'cat': {
      'sub': '123',
      20: ['apple', 'banana', 20]
    },
  };

  print(safe.run(() => v['dog'][0]));
  print(safe.get(v, ['dog', 3, 'sub', 12323, 'error-key']));
  print(safe.set(v, ['dog', 3, 'sub'], '100'));
}

性能测试 #

  • safe.run: 在未捕获错误时, 性能基本无损耗, 捕获错误时, 性能下降 50 倍
  • safe.get/set: 相当于手动判断父级对象是否存在, 性能损耗属于正常逻辑损耗
  // 性能测试: 无任何判断 共耗时 8.312ms
  var p1 = TimeLog.point();
  for (var i = 0; i < 100000; i++) {
    var a = v['dog'][3]['sub'];
  }
  TimeLog.long(p1);

  // 性能测试: try-catch, 无错误时, 共耗时 10.33ms
  var p2 = TimeLog.point();
  for (var i = 0; i < 100000; i++) {
    safe.run(() => v['dog'][3]['sub']);
  }
  TimeLog.long(p2);

  // 性能测试: try-catch, 捕获错误时, 共耗时 563.099ms
  var p3 = TimeLog.point();
  for (var i = 0; i < 100000; i++) {
    safe.run(() => v['dog'][3]['sub']['213213']);
  }
  TimeLog.long(p3);

  // 性能测试: 判断路径安全 共耗时 15.835ms
  var p4 = TimeLog.point();
  for (var i = 0; i < 100000; i++) {
    safe.get(v, ['dog', 3]);
  }
  TimeLog.long(p4);

  // 性能测试: 判断路径安全 共耗时 13.73ms
  var p5 = TimeLog.point();
  for (var i = 0; i < 100000; i++) {
    safe.set(v, ['dog', 2], 'new-dog');
  }
  TimeLog.long(p5);

[0.1.8] - 添加了 dart-doc

[0.1.7] - 更新了 API 链接

[0.1.6] - 更新了 README

[0.1.5] - 使用了更直观的 API 命名

[0.1.4] - 更新了 README

[0.1.3] - 添加通过路径判断的 get/set 方法

[0.1.0] - 添加 example

[0.0.2] - 修改类为函数, 添加 README

  • 修改 class 为 function

[0.0.1] - init

  • TODO: Describe initial release.

example/example.dart

import 'package:safe/safe.dart' as safe;

void main() {
  final dynamic v = {
    'dog': [
      'aa',
      'bb',
      10,
      {'sub': '123'},
      [10, 20]
    ],
    'cat': {
      'sub': '123',
      20: ['apple', 'banana', 20]
    },
  };

  print(safe.run(() => v['dog'][0]));
  print(safe.get(v, ['dog', 3, 'sub', 12323, 'fdasfdsa']));
  print(safe.set(v, ['dog', 3, 'sub'], '100'));

  // 性能测试: 无任何判断 共耗时 8.312ms
  var p1 = TimeLog.point();
  for (var i = 0; i < 100000; i++) {
    var a = v['dog'][3]['sub'];
  }
  TimeLog.long(p1);

  // 性能测试: try-catch, 无错误时, 共耗时 10.33ms
  var p2 = TimeLog.point();
  for (var i = 0; i < 100000; i++) {
    safe.run(() => v['dog'][3]['sub']);
  }
  TimeLog.long(p2);

  // 性能测试: try-catch, 捕获错误时, 共耗时 563.099ms
  var p3 = TimeLog.point();
  for (var i = 0; i < 100000; i++) {
    safe.run(() => v['dog'][3]['sub']['213213']);
  }
  TimeLog.long(p3);

  // 性能测试: 判断路径安全 共耗时 15.835ms
  var p4 = TimeLog.point();
  for (var i = 0; i < 100000; i++) {
    safe.get(v, ['dog', 3]);
  }
  TimeLog.long(p4);

  // 性能测试: 判断路径安全 共耗时 13.73ms
  var p5 = TimeLog.point();
  for (var i = 0; i < 100000; i++) {
    safe.set(v, ['dog', 2], 'new-dog');
  }
  TimeLog.long(p5);
}

class TimeLog {
  static bool isDev = true;

  static int point() {
    return DateTime.now().microsecondsSinceEpoch;
  }

  static void long(num t, [String name = 'Use time: ']) {
    if (isDev) {
      print('$name${(DateTime.now().microsecondsSinceEpoch - t) / 1000}ms');
    }
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  safe: ^0.1.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:safe/safe.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
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]
50
Learn more about scoring.

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

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0

Admin