{"id":21067329,"url":"https://github.com/zefhemel/argspecjs","last_synced_at":"2025-05-16T03:32:16.605Z","repository":{"id":978118,"uuid":"778508","full_name":"zefhemel/argspecjs","owner":"zefhemel","description":"A proper way to handle optional arguments in Javascript","archived":false,"fork":false,"pushed_at":"2015-04-20T11:01:40.000Z","size":130,"stargazers_count":12,"open_issues_count":1,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-19T18:06:28.509Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/zefhemel.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":"2010-07-16T09:06:12.000Z","updated_at":"2018-08-09T17:45:48.000Z","dependencies_parsed_at":"2022-08-16T11:40:38.885Z","dependency_job_id":null,"html_url":"https://github.com/zefhemel/argspecjs","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/zefhemel%2Fargspecjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zefhemel%2Fargspecjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zefhemel%2Fargspecjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zefhemel%2Fargspecjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zefhemel","download_url":"https://codeload.github.com/zefhemel/argspecjs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225402688,"owners_count":17468837,"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-11-19T18:06:32.184Z","updated_at":"2024-11-19T18:06:32.835Z","avatar_url":"https://github.com/zefhemel.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"argspec.js\n==========\n\n`argspec.js` is a simple optional argument handling library for\nJavascript.\n\nThe problem\n-----------\n\nJavascript has no great way of dealing with optional arguments, unless\nthey are optional arguments at the end of the argument list:\n\n    function myFun(a, b, optC) {\n      if(optC === undefined) optC = 0;\n\n      ...\n    }\n\n    myFun(1, 2, 3);\n    myFun(1, 2); // optC will be 0\n\nBut what if you require optional arguments in the beginning of the\nargument list? In the following example `session` and `tx` are both\noptional arguments, but `callback` is required. Dealing with that is\nannoying, as the example shows:\n\n    function list(session, tx, callback) {\n      if(session.call) { // check if first arg is callback function -\u003e first two args were left out\n        callback = session;\n        tx = null;\n        session = null;\n      } else if(session.executeSql) { // check if first arg is transaction object -\u003e first arg was left out\n        callback = tx; \n        tx = session;\n        session = null;\n      }\n\n      ...\n    }\n\nEww.\n\nThe solution\n------------\n\n`argspec.js` offers a declarative way of dealing with optional arguments:\n\n    function list(session, tx, callback) {\n      var args = argspec(arguments, [\n        { name: 'session', optional: true, check: argspec.hasProperty('closeConn') },\n        { name: 'tx', optional: true, check: argspec.hasProperty('executeSql') },\n        { name: 'callback', optional: false, check: argspec.isCallback() }\n      ]);\n      session = args.session;\n      tx = args.tx;\n      callback = args.callback;\n\n      ...\n    }\n\nThe `argspec(args, spec)` method has two arguments:\n\n* `args`\n  An Javascript argument list (typically `arguments`, the built-in Javascript argument list)\n* `spec`\n  An array of objects, defining each of the arguments. Each object has the following properties:\n  * `name`: defines the argument name, is used as the name of the\n    argument in the object returned by the call to the `argspec` function.\n  * `optional` (optional): defines whether the argument is optional (`true`) or not (`false`), default is `false`.\n  * `check` (optional for required arguments): a function that will be passed the argument value and will check if it fits all the requirements. You can pass any function here, `argspec.js` comes with a number of utilty check-function producing functions:\n      * `hasProperty(propname)`: checks if the object has the given property or not\n      * `hasType(typeStr)`: checks if the object has the given type (as given by `typeof obj`)\n      * `isCallback()`: checks if the object is callable\n  * `defaultValue` (optional): a default value, if the argument is left out\n\nThe object returned by `argspec(..)` has a property for each argument.\n\nNote that you can use `argspec.js` to do declarative argument value validation, even if you do not require optional arguments:\n\n    function addNums(a, b) {\n      var args = argspec(arguments, [\n        { name: 'a', check: argspec.hasType('number') },\n        { name: 'b', check: argspec.hasType('number') }\n      ]);\n      return args.a + args.b; // or return a + b; would also be fine in this case\n    }\n\nUsers\n-----\n\nThe `argspec.js` library is used (and included in) the\n[persistence.js](http://github.com/zefhemel/persistencejs) Javascript\nORM library.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzefhemel%2Fargspecjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzefhemel%2Fargspecjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzefhemel%2Fargspecjs/lists"}