项目作者: arg-def

项目描述 :
Object readings and complex transformations using dot notation syntax.
高级语言: TypeScript
项目地址: git://github.com/arg-def/dot-notation.git
创建时间: 2019-09-27T09:26:29Z
项目社区:https://github.com/arg-def/dot-notation

开源协议:MIT License

下载


NOTICE

@arg-def/dot-notation has been move to @cookbook/dot-notation


@arg-def/dot-notation

Object readings and transformations using dot notation syntax

@arg-def/dot-notation"">NPM Version
Build Status
@arg-def/dot-notation"">Downloads Stats
GitHub stars
Known Vulnerabilities
GitHub issues
@arg-def/dot-notation">Awesome
@arg-def/dot-notation"">install size
@arg-def/dot-notation/lib/dot-notation.min.js"">gzip size

Demo

Play around with dot-notation and experience the magic!

Edit @arg-def/dot-notation

Installation

  1. npm install @arg-def/dot-notation --save
  2. #or
  3. yarn add @arg-def/dot-notation

How to use

Picking a value

  1. import dot from '@arg-def/dot-notation';
  2. const source = {
  3. person: {
  4. name: {
  5. firstName: 'John',
  6. lastName: 'Doe'
  7. },
  8. address: [
  9. {
  10. street: 'Infinite Loop',
  11. city: 'Cupertino',
  12. state: 'CA',
  13. postalCode: 95014,
  14. country: 'United States'
  15. },
  16. ]
  17. }
  18. };
  19. dot.pick('person.name', source);
  20. //outputs { firstName: 'John', lastName: 'Doe' }
  21. dot.pick('person.address[0].street', source);
  22. //outputs "Infinite Loop"

Parsing an object

Conventional parsing

  1. import dot from '@arg-def/dot-notation';
  2. const source = {
  3. 'person.name.firstName': 'John',
  4. 'person.name.lastName': 'Doe',
  5. 'person.address[].street': 'Infinite Loop',
  6. 'person.address[].city': 'Cupertino',
  7. 'person.address[].postalCode': 95014,
  8. };
  9. dot.parse(source);
  10. /* outputs
  11. {
  12. person: {
  13. name: {
  14. firstName: 'John',
  15. lastName: 'Doe',
  16. },
  17. address: [
  18. {
  19. street: 'Infinite Loop',
  20. city: 'Cupertino',
  21. postalCode: 95014,
  22. },
  23. ],
  24. },
  25. }
  26. */

With multiple array items

  1. import dot from '@arg-def/dot-notation';
  2. const source = {
  3. 'person.name.firstName': 'John',
  4. 'person.name.lastName': 'Doe',
  5. 'person.address[0].street': 'Infinite Loop',
  6. 'person.address[0].city': 'Cupertino',
  7. 'person.address[0].postalCode': 95014,
  8. 'person.address[1].street': '1600 Amphitheatre',
  9. 'person.address[1].city': 'Mountain View',
  10. 'person.address[1].postalCode': 94043,
  11. };
  12. dot.parse(source);
  13. /* outputs
  14. {
  15. person: {
  16. name: {
  17. firstName: 'John',
  18. lastName: 'Doe',
  19. },
  20. address: [
  21. {
  22. street: 'Infinite Loop',
  23. city: 'Cupertino',
  24. postalCode: 95014,
  25. },
  26. {
  27. street: 'g1600 Amphitheatre',
  28. city: 'Mountain View',
  29. postalCode: 94043,
  30. },
  31. ],
  32. },
  33. }
  34. */

Parsing single key

  1. import dot from '@arg-def/dot-notation';
  2. const source = 'person.name';
  3. const value = 'John Doe';
  4. dot.parseKey(source, value);
  5. /* outputs
  6. {
  7. person: {
  8. name: 'John Doe',
  9. },
  10. }
  11. */