Skip to content

Conversation

@salzhrani
Copy link

add a predicate similar to replacer argument in JSON.stringify to filter results of the serialization

@glenjamin
Copy link
Owner

Hrm, I'm not massively keen on adding a property onto the objects to pass the predicate around, or the amount of repetition this introduces.

What's your exact use-case, perhaps there's another way to do this?

@salzhrani
Copy link
Author

The repetition is to keep the common path fast, it could be DRYer. the use case is as shown in the tests, I have certain keys that can be at arbitrary depths in an object that I would like to exclude from serialization.

@glenjamin
Copy link
Owner

How often are you serialising? Could you just do the exclusion separately before passing the value to toJSON?

This is how I do it in our app, but we exclude top-level keys, rather than deep ones.

I think i'd rather have a closure to pass predicate down than to add properties, interface could be something like:

filtered = transit.withPredicate(filterFn);
filter.toJSON(someObj);

@salzhrani
Copy link
Author

did you mean

filter = transit.withPredicate(filterFn);
filter.toJSON(someObj);

@glenjamin
Copy link
Owner

Yeah, we'd change the reader/writer creation to use a factory function that takes an optional predicate - existing API would be a default invocation of this, and then users would be free to add their own.

@salzhrani
Copy link
Author

what would the new api look like ?

@glenjamin
Copy link
Owner

What you've got there is the right idea for the factory.

Rather than using a prototype, i'd just have function createWriter(predicate). The existing implementation should do var writer = createWriter(false) and in the rep functions you can do if (predicate) m = m.filter(predicate)

@salzhrani
Copy link
Author

Updated and squashed

index.js Outdated
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of exporting createWriter, could you export something like:

function withFilter(predicate) {
  var filteredWriter = createWriter(predicate);
  return {
    toJSON: function(data) {
      return filteredWriter.write(data);
    },
    fromJSON: fromJSON
  }
}

@salzhrani
Copy link
Author

Added

@salzhrani
Copy link
Author

Is everything in order?

@glenjamin
Copy link
Owner

Yes, this looks good.

One final change though! Could you separate the tests into separate tests per data type?

If you don't have time let me know and I'll merge then make the change myself.

Thanks for all the work on this.

@salzhrani
Copy link
Author

I am currently swamped. Thanks for your patience.

glenjamin added a commit that referenced this pull request Aug 19, 2015
@glenjamin glenjamin merged commit 6a51ace into glenjamin:master Aug 19, 2015
@glenjamin
Copy link
Owner

Released as v0.3.0

@salzhrani
Copy link
Author

Woot ... awesome!

@salzhrani salzhrani deleted the ignore branch August 19, 2015 08:17
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

Successfully merging this pull request may close these issues.

3 participants