Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.[index] and {.[index] === xxx} for array like .prop and {.prop === xxx} for object. #58

Open
xpol opened this issue Mar 9, 2017 · 1 comment

Comments

@xpol
Copy link
Contributor

xpol commented Mar 9, 2017

For object we have:

const object = {
  categories: [
    [
      {name: 'apple', image: 'apple.jpg'},
      {name: 'pear', image: 'pear.jpg'}
    ],
    [
      {name: 'keyboard', image: 'keyboard.jpg'},
      {name: 'mouse', image: 'mouse.jpg'}
    ]
  ]
}


describe('jspath', () => {
  it('allows query object .name and return .image', () => {
    const r = JSPath.apply('.categories..{.name === "apple"}.image', object)
    expect(r).toBe(['apple.jpg'])
  })
  it('allows query object props and return parent', () => {
    const r = JSPath.apply('.categories{..name === "apple"}[0]', object)
    expect(r).toEqual([{'name': 'apple', 'image': 'apple.jpg'}, {'name': 'pear', 'image': 'pear.jpg'}])
  })
})

When it come to array how can I do same queries? I have tried:

const array = {
  categories: [
    [
      ['apple', 'apple.jpg'],
      ['pear', 'pear.jpg']
    ],
    [
      ['keyboard', 'keyboard.jpg'],
      ['mouse', 'mouse.jpg']
    ]
  ]
}


  it('should allows query array by value at index (.[0]) and return value at another index (.[1])', () => {
    const r = JSPath.apply('.categories..{.[0] === "apple"}.[1]', array)
    expect(r).toBe(['apple.jpg']) // fails...
  })
  it('should allows query array by index and return parent object', () => {
    const r = JSPath.apply('.categories{..[0] === "apple"}[0]', array)
    expect(r).toEqual([['apple', 'apple.jpg'], ['pear', 'pear.jpg']]) // coincidentally works... 
  })
})
  1. How can I do the same query when inner object become array.
  2. Would it be possible to make .[index] and {.[index] === xxx} works like .prop and {.prop === xxx} for object?
@dfilatov
Copy link
Owner

Arrays have special meaning in jspath, they are treated as nodesets.
So when .categories.. is applied, result will be ['apple', 'apple.jpg', 'pear', 'pear.jpg', 'keyboard','keyboard.jpg', 'mouse', 'mouse.jpg']. There is no way (in the current implementation) to determine what [0] should be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants