Skip to content
This repository has been archived by the owner on Jun 9, 2020. It is now read-only.

squash method not working like in the example #43

Open
qteen opened this issue Jul 28, 2017 · 2 comments
Open

squash method not working like in the example #43

qteen opened this issue Jul 28, 2017 · 2 comments

Comments

@qteen
Copy link

qteen commented Jul 28, 2017

i'm working on a project that need capabilities that universe library have, but i had some trouble implementing the squash method.
This is my code, basically its the same with the example provided in the readme section

universe([
    {date: "2011-11-14T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab", productIDs: ["001"]},
    {date: "2011-11-14T16:20:19Z", quantity: 2, total: 190, tip: 100, type: "tab", productIDs: ["001", "005"]},
    {date: "2011-11-14T16:28:54Z", quantity: 1, total: 300, tip: 200, type: "visa", productIDs: ["004", "005"]},
    {date: "2011-11-14T16:28:54Z", quantity: 3, total: 285, tip: 100, type: "tab", productIDs: ["001", "002", "005"]}
]).then(function(myUniverse){
    myUniverse.query({
        groupBy: 'type', // GroupBy the type key
        select: {
            $count: true, // Count the number of records
            quantity: { // Create a custom 'quantity' column
                $sum: 'quantity' // Sum the quantity column
            },
        },
        // Limit selection to rows where quantity is greater than 50
        filter: {
            tip: {
                $gt: 50
            }
        },
    }).then(function(result) {
          result.squash(null, null, {
            count: '$sum',
            quantity: {
                sum: '$sum'
            }
        },'Total').then(function(resultq) {
                   console.log(JSON.stringify(resultq.data));
            })
    })

universe_squash.txt

and this is the output
[{"key":"Total","value":{"count":4,"quantity":{"0":"sum"}}}]

what i was expecting from what i read in the example is something like the output below
[{"key":"Total","value":{"count":4,"quantity":{"sum":8}}}]

Am i calling the squash method the wrong way or is this a bug?
Sorry for my bad English.

@esjewett
Copy link
Member

esjewett commented Jul 28, 2017

Not sure as I don't know much about universe, but seems like it's probably a bug. If you do this it seems to work:

universe([
    {date: "2011-11-14T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab", productIDs: ["001"]},
    {date: "2011-11-14T16:20:19Z", quantity: 2, total: 190, tip: 100, type: "tab", productIDs: ["001", "005"]},
    {date: "2011-11-14T16:28:54Z", quantity: 1, total: 300, tip: 200, type: "visa", productIDs: ["004", "005"]},
    {date: "2011-11-14T16:28:54Z", quantity: 3, total: 285, tip: 100, type: "tab", productIDs: ["001", "002", "005"]}
]).then(function(myUniverse){
    myUniverse.query({
        groupBy: 'type', // GroupBy the type key
        select: {
            $count: true, // Count the number of records
            $sum: 'quantity' // Sum the quantity column
        },
        // Limit selection to rows where quantity is greater than 50
        filter: {
            tip: {
                $gt: 50
            }
        },
    }).then(function(result) {
          result.squash(null, null, {
            count: '$sum',
            sum: '$sum'
        },'Total').then(function(resultq) {
                   console.log(JSON.stringify(resultq.data));
            })
    })})

Here's a fiddle if you want to try it out: https://codepen.io/anon/pen/oejPpV

I will leave this open in the event that someone wants to try to fix the problem and send a pull request, or that @tannerlinsley has time to take a look at it.

@qteen
Copy link
Author

qteen commented Aug 11, 2017

After a couple of try and error, i believe this bug is because of a set method in the file lodash.js. I have able to fix this bug, and i've made the pull request #44

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

No branches or pull requests

2 participants