object_path 0.0.1-dev.1

object_path #

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

Still in development, will be ready very soon (1 week max) #

This is a Dart copy of the js projects set-value by Jon Schlinkert and object-path by Mario Casciaro. Please consider following either (or all) projects and giving some stars ⭐ 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 functionality in-line with the originals as far as possible. Bear in mind that some functionality is slightly different to accommodate for the typed nature of Dart, as well as my personal preference (if you want anything changed...my arm can be twisted).

Lastly, please consider following the project author Ferdinand Steenkamp.

Install #

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

dependencies:
    object_path:

Then run flutter pub get.

Usage #

Import the package:

import 'package:object_path/object_path.dart';

Note: I made strong distinctions between list and dot-notation to prevent confusion.

Using dot notation:

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

    // escape split character
    mock = objPath.setDot(mock, 'a.b.c\\.d', 'Value');
}

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 obviously 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 objPath = ObjectPath();
    
    // set a value in a map
    mock = objPath.set(mock, ['a', 'b', 'c'], 'Value');
    
    // unset value in a map
    mock = objPath.unset(mock, ['a', 'b', 'c']);
    
    //get value in a map, returns null if it doesn't exists
    print(objPath.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.

0.0.1-dev.1 #

  • Actively developing, not ready for use

example/object_path_example.dart

import 'package:object_path/object_path.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 objPath = ObjectPath();

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

  mock = objPath.setList(mock, ['Key 4'], 'Val 4');
  mock = objPath.setList(mock, ['Key 5', 'Key 5.1'], 'Val 5.1');

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

  mock = objPath.unsetList(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'}
  };

  objPath = ObjectPath();

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

  mock = objPath.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 = objPath.setDot(mock, 'Key 5.Key 5\\.1', 'Val 5.1');

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

  mock = objPath.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(objPath.getList(mock, ['Key 5','Key 5.1']));
  print(objPath.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:
  object_path: ^0.0.1-dev.1

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:object_path/object_path.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]
85
Overall:
Weighted score of the above. [more]
47
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Format lib/src/object_path_base.dart.

Run dartfmt to format lib/src/object_path_base.dart.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Package is pre-release. (-5 points)

Pre-release versions should be used with caution; their API can change in breaking ways.

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