JSPM

@ngha/nested-value

0.2.5
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 5
    • Score
      100M100P100Q47235F
    • License MIT

    nestedValue the value at `path` of `object`

    Package Exports

    • @ngha/nested-value

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@ngha/nested-value) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    nested-value

    This library get object value by path

    Usage

    Import NestedValueModule to your module

    import { NgModule } from '@angular/core';
    import { BrowserModule  } from '@angular/platform-browser';
    import { AppComponent } from './app';
     
    import { NestedValueModule } from '@ngha/nested-value';
    @NgModule({
      imports: [NestedValueModule],
    })
    export class AppModule {}

    nested-value

    Returns value of an object value

    Usage: object | nestedValue: 'path' : 'defaultValue'

      <p>{{{ 'a': 'aa' } | nestedValue:'a'}}</p>
      <!-- Output: "aa" -->
      <p>{{{ c: [{ d: '123', e: 'abc' }, { d: '456', e: 'def' }] } | nestedValue:'c[0].d'}}</p>
      <!-- Output: "123" -->
      <p>{{{ c: [{ d: '123', e: 'abc' }, { d: '456', e: 'def' }] } | nestedValue:['c', [1], 'e']}}</p>
      <!-- Output: "def" -->
      <p>{{{ c: [{ d: '123', e: 'abc' }, { d: '456', e: 'def' }] } | nestedValue:['c', [3], 'f']: 'default'}}</p>
      <!-- Output: "default" -->
      <p>{{{ c: [{ d: '123', e: 'abc' }, { d: '456', e: 'def' }] } | nestedValue:['c', [3], 'f']}}</p>
      <!-- Output: undefine -->
     import {NestedValueService} from '@ngha/nested-value'
     
     @Component({
       providers: [TransformObjectService],
     })
     export class AppComponent
     {
       constructor(private service: TransformObjectService)
       {
         const obj = {
          aa: [{ b: { c: 0 }, 1: 0 }],
          dd: { ee: { ff: 2 } },
          gg: { h: 2 },
          'gg.h': 1,
          'kk.ll': { 'mm.n': [3, 4, { 'oo.p': 5 }, { 'oo': { p: 6 } }] },
          tt: null
         };
    
          const result1 = this.service.nestedValue(obj , 'aa[0].b.c' ) === obj['aa'][0]['b']['c'] 
          // Output: result1 = true
          const result2 = this.service.nestedValue(obj , 'aa[0][1]') === obj['aa'][0][1]
          // Output: result2 = true
          const result3 = this.service.nestedValue(obj , ['kk.ll', 'mm.n', 2, 'oo.p']) === obj['kk.ll']['mm.n'][2]['oo.p']
          // Output: result3 = true
          const result4 = this.service.nestedValue(obj , ['kk.ll', 'mm.n', 3, 'oo', 'p']) === 6
          // Output: result4 = true
          const result5 = this.service.nestedValue(undefine , ['kk'] , 1) === 1
          // Output: result5 = true
          const result6 = this.service.nestedValue(undefine , 'kk') === undefine
          // Output: result6 = true
    
       }
     
     }