z-pattern-matching / z

Pattern Matching for Javascript
https://z-pattern-matching.github.io/
Apache License 2.0
1.72k stars 50 forks source link

Working with "dynamic" objects #41

Closed ptusch closed 6 years ago

ptusch commented 6 years ago

Hey there,

I'm trying to get a grip on this library and would love to use it with objects. Currently, I'm trying to parse a version and based on the object's major/minor/patch, do different things:

'use strict';

const matches = require('z').matches;
const semver = require('semver-utils');

const manual = {
  major: '1',
};
const parsed = semver.parse('1.0.0');

// Always default
matches(parsed).call(parsed,
  (x = {major: '1'}) => console.log('m_one'),
  (x = {major: '2'}) => console.log('m_two'),
  (x = null) => console.log('default'),
  (x) => console.dir(x)
)

// Always 1 but static :(
matches(manual).call(manual,
  (x = {major: '1'}) => console.log('m_one'),
  (x = {major: '2'}) => console.log('m_two'),
  (x = null) => console.log('default'),
  (x) => console.dir(x)
)

Following the sample from the documentation results that Marie is welcome - it works as expected and my implementation for manual does, too. But that is basic stuff I don't need. I'd like to patternmatch things dynamically. I'm not sure if it's possible of if I'm just using the library in the wrong manner.

I've read about the .call approach in other issues but this doesn't seem to help.

Help is much appreciated!

leonardiwagner commented 6 years ago

Hi @ptusch , you are right, currently is only matching EXACT objects. This feature about matching "dynamic objects" was on the roadmap for a while but is not implemented yet.

I'll give it high priority and I'm going try to release that until the end of the day!

ptusch commented 6 years ago

Wow, thanks @leonardiwagner it is much appreciated!

leonardiwagner commented 6 years ago

Hey @ptusch this feature was implemented, many thanks for reporting that! :tada:

Please use the new version z@1.0.6 and give me a feedback if it's working as expected now, or if still not working for you.

ptusch commented 6 years ago

Works as intended now, thanks. Kudos for this lightning fast reaction! Thats really nice! :+1: