{"id":13451930,"url":"https://github.com/z-pattern-matching/z","last_synced_at":"2025-05-14T20:04:52.822Z","repository":{"id":39759852,"uuid":"41743960","full_name":"z-pattern-matching/z","owner":"z-pattern-matching","description":"Pattern Matching for Javascript","archived":false,"fork":false,"pushed_at":"2023-10-28T18:22:35.000Z","size":521,"stargazers_count":1716,"open_issues_count":23,"forks_count":48,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-04-06T15:01:35.235Z","etag":null,"topics":["functional-programming","immutability","pattern-matching"],"latest_commit_sha":null,"homepage":"https://z-pattern-matching.github.io/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/z-pattern-matching.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-09-01T14:37:18.000Z","updated_at":"2025-03-14T06:36:07.000Z","dependencies_parsed_at":"2024-01-15T17:49:54.478Z","dependency_job_id":null,"html_url":"https://github.com/z-pattern-matching/z","commit_stats":{"total_commits":93,"total_committers":11,"mean_commits":8.454545454545455,"dds":"0.30107526881720426","last_synced_commit":"39c35d920e546d562634c6755d772d95afeeeba9"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/z-pattern-matching%2Fz","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/z-pattern-matching%2Fz/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/z-pattern-matching%2Fz/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/z-pattern-matching%2Fz/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/z-pattern-matching","download_url":"https://codeload.github.com/z-pattern-matching/z/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248751822,"owners_count":21155964,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["functional-programming","immutability","pattern-matching"],"created_at":"2024-07-31T07:01:07.277Z","updated_at":"2025-04-13T17:27:27.611Z","avatar_url":"https://github.com/z-pattern-matching.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","📦 Legacy \u0026 Inactive Projects"],"sub_categories":[],"readme":"# ![z](https://raw.githubusercontent.com/leonardiwagner/z/master/z-logo.png) Pattern matching for JavaScript\n\n[![Build Status](https://travis-ci.org/z-pattern-matching/z.svg?branch=master)](https://travis-ci.org/z-pattern-matching/z)\n[![Coverage Status](https://coveralls.io/repos/github/z-pattern-matching/z/badge.svg?branch=master)](https://coveralls.io/github/z-pattern-matching/z?branch=master)\n[![NPM version](https://img.shields.io/npm/v/z.svg)](https://www.npmjs.com/package/z)\n\n### Usage\n- Install via npm: `npm install z`\n- Require z in your code and use the matches function: `const { matches } = require('z')`\n\n### Avaiable Patterns\n\n- Matches by value: `(x = 1) =\u003e, (x = null) =\u003e, (x = 'true') =\u003e`\n- Matches by object or array: `(x = {a: 1}) =\u003e, (x = [1, 2]) =\u003e`\n- Matches by type: `(x = String) =\u003e, (x = Boolean) =\u003e`\n- Matches by instance: `(x = Date) =\u003e, (x = Person) =\u003e`\n- Matches by splitting array into elements and tail `(head, tail) =\u003e` , `(a, b, c, tail) =\u003e`, etc…\n\n\n### Examples\n- **Example:** Matches by Object property\n```javascript\nconst { matches } = require('z')\n\nconst person = { name: 'Maria' }\nmatches(person)(\n  (x = { name: 'John' }) =\u003e console.log('John you are not welcome!'),\n  (x)                    =\u003e console.log(`Hey ${x.name}, you are welcome!`)\n)\n\n//output: `Hey Maria, you are welcome!`\n```\n\n- **Example:** Matches by type or instances\n```javascript\nconst { matches } = require('z')\n\nconst result = matches(1)(\n  (x = 2)      =\u003e 'number 2 is the best!!!',\n  (x = Number) =\u003e `number ${x} is not that good`,\n  (x = Date)   =\u003e 'blaa.. dates are awful!'\n)\n\nconsole.log(result) // output: number 1 is not that good\n```\n\n- **Example:** matches Array content\n\n\u003e To match array content you need create multiple arguments for the match function, such as (a, b, c, tail) =\u003e {} , then each variable match each item from array. Note: last variable contains all remaining array items, formally named tail. Examples:\n```javascript\nconst { matches } = require('z')\n\nmatches([1, 2, 3, 4, 5])(\n  (a, b, c, tail) =\u003e 'a = 1, b = 2, c = 3, tail = [4, 5]'  \n)\n\nmatches([1, 2])(\n  (a, tail) =\u003e 'a = 1, b = [2]'  \n)\n\nmatches([1])(\n  (a, b,  tail)       =\u003e 'Will not match here',\n  (a = 2, tail = [])  =\u003e 'Will not match here',\n  (a = 1, tail = [])  =\u003e 'Will match here, tail = []'\n)\n```\n\n- **Example:** Powerful recursive code which will remove sequential repeated items from Array.\n\n\u003e Can be mind blowing if it’s the first time you meet pattern matching, but you are gonna understand it!\n```javascript\nconst { matches } = require('z')\n\nconst compress = (numbers) =\u003e {\n  return matches(numbers)(\n    (x, y, xs) =\u003e x === y\n      ? compress([x].concat(xs))\n      : [x].concat(compress([y].concat(xs))),\n    (x, [y]) =\u003e x === y // stopping condition\n      ? [x]\n      : [x, y],\n    x =\u003e x\n  )\n}\n\ncompress([1, 1, 2, 3, 4, 4, 4]) //output: [1, 2, 3, 4]\n```\n\n### License\n\n[Apache 2.0][apache-license]\n\n[apache-license]:./LICENSE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fz-pattern-matching%2Fz","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fz-pattern-matching%2Fz","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fz-pattern-matching%2Fz/lists"}