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

Suggestion: Generate Param values given a JSON #42

Open
rmrbytes opened this issue Dec 17, 2022 · 5 comments
Open

Suggestion: Generate Param values given a JSON #42

rmrbytes opened this issue Dec 17, 2022 · 5 comments

Comments

@rmrbytes
Copy link

I came across this library while searching for just this functionality. Thanks for sharing it.

I was wondering whether it would be possible to kind of reverse engineer and determine the parameter values given a resultant json.

Let me give an example to clarify what I am seeking:

Suppose the template is defined as

const template = parse("{
    title: 'Fixed Title",
   {foo:baz}
 }");

And passing a foo value generates the json

console.log(template({foo: 'baz'}) // prints {title:'Fixed Title', foo: 'baz'}

But is there a way to get the params given the resultant json. I am using a function called extract as an example to explain what will be useful

console.log(template.extract({title: 'Fixed Title', foo: 'baq'}) // prints {foo: 'baq'} 

Thanks

@curran
Copy link
Member

curran commented Dec 17, 2022

Interesting idea! It might be possible.

I would welcome a PR if you'd like to attempt a solution.

Do you have any initial thoughts on how a solution might be implemented?

It reminds me of this JSON diff library I've worked with https://github.com/kbadk/json0-ot-diff

@rmrbytes
Copy link
Author

Thanks @curran for your response. I am doing something like this.

const extractJson = (templateJson, resultJson) => {
    // get template
    const template = parse(templateJson)
    // get params
    const params = template.parameters
    // construct extract json
    let _ejson = {}
    // loop through params
    for (let i = 0; i < params.length; i++) {
        const _keyname = params[i].key
        // findKey of _keyname
        const _arr = findKey(_new, _keyname)
        // check not null
        if (_arr.length > 0) {
            _ejson[_keyname] = _arr[0]
        }
        else {
            _ejson[_keyname] = null
        }
        return _ejson
  }
}

// find key function
const findKey = (obj, keyname) => {
    let result = []
    const mergeArray = (s1, s2) => result = [...s1, ...s2];
    if (Array.isArray(obj)) {
        obj.forEach(o => {
            mergeArray(result, findKey(o, keyname))
        })
    } else {
        if (typeof obj === 'object') {
            Object.keys(obj).forEach(k => {
                if (k === keyname) {
                    result.push(obj[k])
                }
                if (obj[k] !== undefined && obj[k] !== null)
                    mergeArray(result, findKey(obj[k], keyname))
            })
        }
    }
    return result
}

@rmrbytes
Copy link
Author

or if you already have the template

const extractJson = (template, resultJson) => {
    // get params
    const params = template.parameters
    // construct extract json
    let _ejson = {}
    // loop through params
    for (let i = 0; i < params.length; i++) {
        const _keyname = params[i].key
        // findKey of _keyname
        const _arr = findKey(_new, _keyname)
        // check not null
        if (_arr.length > 0) {
            _ejson[_keyname] = _arr[0]
        }
        else {
            _ejson[_keyname] = null
        }
        return _ejson
  }
}

@curran
Copy link
Member

curran commented Dec 19, 2022

Awesome! Happy to accept a PR if you add tests and docs.

@rmrbytes
Copy link
Author

Hello @curran :

While I did send you PR, I realise that it has a limitation. In the above method, I am assuming that the context object (the input json) key is the same name as the parameter. I did not account for cases where that may not be true. In which case, the PR sent will fail. Obviously, this is an after thought and hence I do not have any test case that checks for this too.

I will think up of some other compare logic to determine locating the param in the context object

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