Skip to content

Latest commit

 

History

History
87 lines (50 loc) · 2.65 KB

README.md

File metadata and controls

87 lines (50 loc) · 2.65 KB

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.