{"id":22505150,"url":"https://github.com/azer/new-struct","last_synced_at":"2025-08-03T11:32:45.503Z","repository":{"id":9816617,"uuid":"11799980","full_name":"azer/new-struct","owner":"azer","description":"Structs inspred from Golang","archived":false,"fork":false,"pushed_at":"2014-08-17T22:20:28.000Z","size":251,"stargazers_count":17,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-01T02:23:50.152Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/azer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-07-31T20:04:39.000Z","updated_at":"2021-12-16T14:17:02.000Z","dependencies_parsed_at":"2022-09-05T05:11:47.538Z","dependency_job_id":null,"html_url":"https://github.com/azer/new-struct","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/azer%2Fnew-struct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azer%2Fnew-struct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azer%2Fnew-struct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azer%2Fnew-struct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/azer","download_url":"https://codeload.github.com/azer/new-struct/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228540830,"owners_count":17934030,"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-07T00:15:02.705Z","updated_at":"2024-12-07T00:15:03.400Z","avatar_url":"https://github.com/azer.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## new-struct\n\nA minimalistic class system designed for flexibility, functional programming. Inspired from Golang's Struct concept. ([Blog Post](https://medium.com/p/8e5459ce9467))\n\n### Motivation\n\n* It's quite simple and small.\n* All it does is composing functions and objects.\n* It doesn't have `new` and `this` keywords. So, you'll never have to fix scopes.\n* You can do currying, partial programming with both `new-struct` and structs.\n\n### How does a struct look like?\n\n```js\nvar struct = require('new-struct')\n\nvar Animal = struct({\n  sleep: sleep,\n  speak: speak\n})\n\nmodule.exports = Animal;\n\nfunction sleep (animal) {\n  console.log('zzzz');\n}\n\nfunction speak (animal, text) { \n  console.log('%s says %s', animal.name, text);\n}\n```\n\nAnd this is how you would call it:\n\n```js\ndongdong = Animal({ name: 'dongdong' })\n\ndongdong.name\n// =\u003e 'dong dong'\n\ndongdong.run()\n// dongdong is running\n\nblackbear.sleep()\n// blackbear is sleeping\n```\n\nCheck out [Usage](#usage) and examples for more info about it.\n\n## Install\n\n```bash\n$ npm install new-struct\n```\n\n## Usage\n\nA new struct is defined by an object of methods:\n\n```js\nStruct = require('new-struct')\n\nAnimal = Struct({\n  sleep: sleep,\n  run: run,\n  speak: speak\n})\n\nfunction sleep (animal) {\n  console.log('zzz');\n}\n\nfunction speak (animal, sound) {\n  console.log('%s says %s', animal.name, sound)\n}\n\nfunction run (animal) {\n  console.log('%s is running', animal.name)\n}\n```\n\nTo create a an instance of the `Animal` struct, just call it with an object.\n`this` and `new` keywords are not needed, everything is just functions.\n\n```\ndongdong = Animal({ name: 'dongdong' })\nblackbear = Animal({ name: 'blackbear' })\n\ndongdong.name\n// =\u003e 'dong dong'\n\ndongdong.run()\n// dongdong is running\n\nblackbear.sleep()\n// blackbear is sleeping\n```\n\n### Factory Functions\n\nIt doesn't support constructors, but constructor-like factory functions are easy to implement:\n\n```js\nfunction NewAnimal (name, age) {\n  return Animal({ name: name, age: age })\n}\n```\n\nNote that you can attach your constructor as a static method. So, you could have such a module:\n\n```js\nAnimal = Struct({\n  New: New,\n  run: run,\n  speak: speak\n})\n\nmodule.exports = Animal;\n\nfunction New (name, age) {\n  return Animal({ name: name, age: age })\n}\n\nfunction speak (animal, sound) {\n  console.log('%s says %s', animal.name, sound)\n}\n\nfunction run (animal) {\n  console.log('%s is running', animal.name)\n}\n```\n\nThis will allow other modules requiring this have more flexibility:\n\n```js\nAnimal = require('./animal')\n\n// You can either create using constructor:\nAnimal.New('dong dong', 13)\n\n// Or calling the constructor itself:\nAnimal({ name: 'dong dong', age: 13 })\n\n// You can also access the methods of Animal:\nAnimal.methods.run({ name: 'black bear' })\n// will output: black bear is running\n```\n\n### Mixing\n\nYou can create structs that mixes other ones:\n\n```js\nAnimal = require('./animal')\n\nCat = Struct(Animal, {\n  meow: meow\n})\n\nfunction meow (cat) {\n  Animal.methods.speak(cat, 'meooww')\n}\n```\n\nNotice that each struct has a property called `methods` that keeps all the functions passed to it, including the ones derived from other structs.\n\nSee the tests and examples for more info, or create issues \u0026 send pull requests to improve the documentation.\n\n![](http://i.cloudup.com/CZR70W5Sct.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazer%2Fnew-struct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fazer%2Fnew-struct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazer%2Fnew-struct/lists"}