{"id":23366699,"url":"https://github.com/d8corp/mazzard","last_synced_at":"2025-04-07T23:16:03.136Z","repository":{"id":26870580,"uuid":"111447641","full_name":"d8corp/mazzard","owner":"d8corp","description":null,"archived":false,"fork":false,"pushed_at":"2023-01-04T21:37:51.000Z","size":450,"stargazers_count":0,"open_issues_count":13,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T07:46:36.658Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/d8corp.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}},"created_at":"2017-11-20T18:30:29.000Z","updated_at":"2020-04-12T18:35:40.000Z","dependencies_parsed_at":"2023-01-14T05:27:21.805Z","dependency_job_id":null,"html_url":"https://github.com/d8corp/mazzard","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d8corp%2Fmazzard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d8corp%2Fmazzard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d8corp%2Fmazzard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d8corp%2Fmazzard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/d8corp","download_url":"https://codeload.github.com/d8corp/mazzard/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247577702,"owners_count":20961170,"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":[],"created_at":"2024-12-21T14:17:19.839Z","updated_at":"2025-04-07T23:16:03.110Z","avatar_url":"https://github.com/d8corp.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mazzard\n### Install\n```bash\nnpm i mazzard\n```\nNext, we will use `mazzard` which means we imported it from `mazzard`.\n```javascript\nimport mazzard from 'mazzard'\n\nmazzard() // undefined\n```\n\n### Unexpected arguments\n`mazzard` returns the first argument as is, if it is not `function`, `array` or `simple object`.\n```javascript\nmazzard(1) // 1\nmazzard('1') // '1'\nmazzard(false) // false\nmazzard(null) // null\nmazzard(NaN) // NaN\nmazzard(Symbol('test')) // Symbol('test')\nmazzard(new Map()) // instance of Map\n```\n\n### Observable object\nYou get the observable object if the first argument is `simple object`.\n```javascript\nconst observable = mazzard({test: 'success'})\nconsole.log(observable.test)\n// \u003e 'success'\n```\nAll objects inside observable are observable.\n```javascript\nconst test = mazzard({observableField: {}})\ntest // is observable object\ntest.observableField // is observable object\n```\n\n### Observer\nTo have reactions on changes of observable object, use `mazzard` with a function as the first argument.\nThe function is observer and runs immediately.\n```javascript\nconst test = mazzard({})\n\nmazzard(\n  () =\u003e console.log(test.testField) // this is observer\n)\n// \u003e undefined\n\ntest.testField = 'success'\n// \u003e 'success'\n```\nObserver runs each time when you change observable fields.  \nYou may stop the watching with the first argument of observer.\n```javascript\nconst test = mazzard({})\n\nmazzard(stop =\u003e {\n  if (test.stop) {\n    console.log('stop', test.stop)\n    stop()\n  } else {\n    console.log(test.testField)\n  }\n})\n// \u003e undefined\n\ntest.testField = 'success'\n// \u003e 'success'\n\ntest.testField = true\n// \u003e true\n\ntest.stop = 'test message'\n// \u003e 'stop', 'test message'\n\ntest.testField = 'test'\n// nothing happens\n```\nAlso, you may stop it with that mazzard returns.\n```javascript\nconst stop = mazzard(() =\u003e {})\nstop()\n```\nIf you set the same value which a field have then reaction will not be called.\n```javascript\nconst test = mazzard({})\n\nmazzard(() =\u003e console.log(test.testField))\n// \u003e undefined\n\ntest.testField = true\n// \u003e true\n\ntest.testField = true\n// nothing happens\n```\n### United changes\nIf you wanna have only one reaction of observer on several changes, you may use a method of observable object.\n```javascript\nconst test = mazzard({\n  update (field1, field2) {\n    test.field1 = field1\n    test.field2 = field2\n  }\n})\n\nmazzard(() =\u003e {\n  console.log(test.field1, test.field2)\n})\n// \u003e undefined, undefined\n\ntest.field1 = 'field1'\n// \u003e 'field1', undefined\n\ntest.field2 = 'field2'\n// \u003e 'field1', 'field2'\n\ntest.update(1, 2)\n// \u003e 1, 2\n```\nYou may unite changes with `action` from `mazzard`.\n```javascript\nimport {action} from 'mazzard'\n\nconst test = mazzard({})\n\nconst update = action((field1, field2) =\u003e {\n  test.field1 = field1\n  test.field2 = field2\n})\n\nmazzard(() =\u003e console.log(test.field1, test.field2))\n// \u003e undefined, undefined\n\nupdate(1, 2)\n// \u003e 1, 2\n```\nThe same will happen for setters.\n```javascript\nconst test = mazzard({\n  set fullName (value) {\n    const [name, secondName] = value.split(' ')\n    this.name = name\n    this.secondName = secondName\n  }\n})\n\nmazzard(() =\u003e console.log(test.name, test.secondName))\n// \u003e undefined, undefined\n\ntest.fullName = 'Mike Mighty'\n// \u003e 'Mike', 'Mighty'\n```\n### Computed value\nUse getters in observable to have computed value with caching.\n```javascript\nconst test = mazzard({\n  get fullName () {\n    return this.name \u0026\u0026 this.secondName ? `${this.name} ${this.secondName}` : null\n  }\n})\n\nmazzard(() =\u003e console.log(test.fullName))\n// \u003e null\n\ntest.name = 'Mike'\n// nothing happens\n\ntest.secondName = 'Mighty'\n// \u003e 'Mike Mighty'\n```\n### Observable array\nYou get the observable array if the first argument is `array`.\n```javascript\nmazzard([]) // observable array\n```\nAll changes on the array make reactions.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fd8corp%2Fmazzard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fd8corp%2Fmazzard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fd8corp%2Fmazzard/lists"}