Inist-CNRS / node-jbj

Like XSL/XML but for JSON.
http://Inist-CNRS.github.io/jbj-playground/
13 stars 3 forks source link

Tentative d'ajout d'un exemple pour sortBy #21

Closed kerphi closed 8 years ago

kerphi commented 8 years ago

Mais je ne mets pas directement dans master car ça ne fonctionne pas comme je l'attendais. En sortie on a étrangement ceci :

{
  "0": {
    "name": "abcd"
  },
  "1": {
    "name": "zert"
  }
}
nojhamster commented 8 years ago

I'm suspecting this line to cast arrays into simple objects.

parmentf commented 8 years ago

D'accord avec toi, ce n'est pas naturel (et il ne doit pas être facile d'obtenir ce qu'on veut, après ça).

parmentf commented 8 years ago

Pourtant, sort n'a l'air défini que sur des tableaux, pas sur des objets (bon, un tableau est un objet). En castant l'objet en tableau, ça correspond bien.

Array({
  "0": {
    "name": "abcd"
  },
  "1": {
    "name": "zert"
  }
});
nojhamster commented 8 years ago

sort is defined because Object.create() copies the properties from the array to the new object.

Assuming sort / sortBy are only meant to be used on arrays, wouldn't it be simpler to check the type with Array.isArray() and use obj.slice().sort() ?

touv commented 8 years ago

no solution yet , but the origin :https://github.com/tj/ejs/blob/master/lib/filters.js#L61

touv commented 8 years ago

object2array : https://github.com/Inist-CNRS/node-jbj/pull/22

parmentf commented 8 years ago

I would have just cast the Object to an Array, but this works too.

parmentf commented 8 years ago

Now that it's been fixed, this documentation is correct.