set_value 1.0.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

set_value #

Create nested values and any intermediaries using list notation ['a','b',c'] OR dot notation "a.b.c".

This is a Dart copy of the js project set-value by Jon Schlinkert. Please consider following either (or both) projects and starring the projects to show your ❤️ and support. Also, feel free to make pull requests, open issues, or just let me know if I've messed up. The goal is to keep this project in-line with the original as far as possible. Lastly, please consider following the project author Ferdinand Steenkamp.

Install #

Install with pub.dev, add this to your pubspec.yaml:

dependencies:
    set_value:

Then run flutter pub get.

Usage #

Import the package:

import 'package:set_value/set_value.dart';

Using dot notation: #

void main() {
    
    var mock = <String, dynamic>{}
    var setValue = SetValue();
    
    // set a value in a map
    mock = setValue.setDot(mock, 'a.b.c', 'Value', splitAt = '.', escapeWith = '\\');
    
    // unset value in a map
    mock = setValue.unsetDot(mock, 'a.b.c');
    
    //get value in a map, returns null if it doesn't exists
    print(setValue.getDot(mock, 'a.b.c'));
}

Notes:

Escaping: You can escape a split character using \\, alternatively, you can add your own escape character using the optional escapeWith option.

Dot Notation: The dot notation oviously uses...dots. Alternatively, you can add your own dot character using the optional splitAt option.

Using List<String> method: #

void main() {
    
    var mock = <String, dynamic>{}
    var setValue = SetValue();
    
    // set a value in a map
    mock = setValue.set(mock, ['a', 'b', 'c'], 'Value');
    
    // unset value in a map
    mock = setValue.unset(mock, ['a', 'b', 'c']);
    
    //get value in a map, returns null if it doesn't exists
    print(setValue.get(mock, ['a','b','c']));
}

About #

Pull requests and issues always welcome. There is a lot more information on this project at the original set-value.

Author #

Ferdinand Steenkamp

License #

Copyright © 2020, Ferdinand Steenkamp. Released under the BSD License.

1.0.4 #

  • Fixed docs

1.0.3 #

  • Added get and getDot functions

1.0.2 #

  • Added more optional params
  • Fixed small bugs
  • Added dot notation
  • Added tests
  • Document fixes

1.0.1 #

  • Document fix

1.0.0 #

  • Initial version, created by Stagehand

example/set_value_example.dart

import 'package:set_value/set_value.dart';
import 'package:pretty_json/pretty_json.dart';

void main() {
  //List Example
  var mock = <String, dynamic>{
    'Key 1': 'Val 1',
    'Key 2': 'Val 2',
    'Key 3': {'Key 3.1': 'Val 3'}
  };

  var setValue = SetValue();

  /*
  * Below we set three different keys on the map,
  * two of which are nested.
  * */

  mock = setValue.set(mock, ['Key 4'], 'Val 4');
  mock = setValue.set(mock, ['Key 5', 'Key 5.1'], 'Val 5.1');

  /*
  * Below we remove Key 2 and it's value from the map
  * */

  mock = setValue.unset(mock, ['Key 2']);

  /*
  * Here we expect an output of:
  * {
  *   'Key 1': 'Val 1',
  *   'Key 3': {
  *     'Key 3.1': 'Val 3'
  *   }
  *   'Key 4': 'Val 4',
  *   'Key 5':  {
  *     'Key 5.1': 'Val 5.1',
  *   }
  * }
  * */

  print(prettyJson(mock));

  //Dot Notation Example
  //reset the Map
  mock = <String, dynamic>{
    'Key 1': 'Val 1',
    'Key 2': 'Val 2',
    'Key 3': {'Key 3.1': 'Val 3'}
  };

  setValue = SetValue();

  /*
  * Below we set three different keys on the map,
  * two of which are nested.
  * */

  mock = setValue.setDot(mock, 'Key 4', 'Val 4');

  //Remember to escape unwanted splits using \\
  //Alternatively, you can specify your own split character using the
  //option 'splitAt' param
  mock = setValue.setDot(mock, 'Key 5.Key 5\\.1', 'Val 5.1');

  /*
  * Below we remove Key 2 and it's value from the map
  * */

  mock = setValue.unsetDot(mock, 'Key 2');

  /*
  * Here we expect an output of:
  * {
  *   'Key 1': 'Val 1',
  *   'Key 3': {
  *     'Key 3.1': 'Val 3'
  *   }
  *   'Key 4': 'Val 4',
  *   'Key 5':  {
  *     'Key 5.1': 'Val 5.1',
  *   }
  * }
  * */

  print(prettyJson(mock));

  print(setValue.get(mock, ['Key 5','Key 5.1']));
  print(setValue.getDot(mock, 'Key 5.Key 5\\.1'));
}

Use this package as a library

1. Depend on it

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


dependencies:
  set_value: ^1.0.4

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:set_value/set_value.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
99
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 May 30, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.2
  • pana: 0.13.8-dev

Health suggestions

Fix lib/src/set_value_base.dart. (-1 points)

Analysis of lib/src/set_value_base.dart reported 2 hints:

line 113 col 9: The value of the local variable 'orig' isn't used.

line 125 col 9: Dead code.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
Dev dependencies
pedantic ^1.8.0
pretty_json any
test ^1.6.0