{"id":15705867,"url":"https://github.com/kyleross/autopurge","last_synced_at":"2025-06-20T12:06:28.508Z","repository":{"id":34955974,"uuid":"39031238","full_name":"KyleRoss/AutoPurge","owner":"KyleRoss","description":"Automatically purge old items from an array once it hits a certain length","archived":false,"fork":false,"pushed_at":"2015-07-14T16:44:54.000Z","size":136,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-30T00:43:34.256Z","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/KyleRoss.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","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":"2015-07-13T18:56:47.000Z","updated_at":"2024-08-11T02:02:40.000Z","dependencies_parsed_at":"2022-09-01T23:40:17.515Z","dependency_job_id":null,"html_url":"https://github.com/KyleRoss/AutoPurge","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/KyleRoss%2FAutoPurge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KyleRoss%2FAutoPurge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KyleRoss%2FAutoPurge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KyleRoss%2FAutoPurge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KyleRoss","download_url":"https://codeload.github.com/KyleRoss/AutoPurge/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246342799,"owners_count":20761939,"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-10-03T20:20:32.060Z","updated_at":"2025-03-30T15:45:18.290Z","avatar_url":"https://github.com/KyleRoss.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Auto Purge\nAutomatically purge items from an array when the maxlength is hit. This module can be used in Node or the browser. Browser support is for modern ES5 browsers (IE9+, Chrome, Firefox, etc.)\n\n**Why?** There are certain cases where you want a rolling array of values that are limited such as a log system or chat. Once the array fills up to the maxlength, the oldest records are purged from the array.\n\n## Getting Started\nInstall the AutoPurge module.\n\n### Node\n\n    npm install autopurge\n\n```javascript\nvar AutoPurge = require('autopurge');\n```\n\n### Bower\n\n    bower install autopurge\n\n### Without Package Manager\nDownload `autopurge.js` or `autopurge.min.js` in the repository and include in your application.\n\n### Browser\n\n```html\n\u003cscript type=\"text/javascript\" src=\"path/to/autopurge.min.js\"\u003e\u003c/script\u003e\n```\n\n## Example\n\n```javascript\nvar myArray = ['test', 'value', true, false, 1, 20],\n    purge = new AutoPurge(10, myArray);\n\npurge.push('new value'); // =\u003e 0 (no records purged)\npurge.length; // =\u003e 7\npurge.value; // =\u003e ['test', 'value', true, false, 1, 20, 'new value']\npurge.push.apply(purge, [1, 2, 3, 4, 5]); // =\u003e 2 (records purged)\npurge.value; // =\u003e [true, false, 1, 20, 'new value', 1, 2, 3, 4, 5]\n\npurge.purge(2); // =\u003e [true, false]\npurge.length; // =\u003e 8\n\n// Array modified outside of AutoPurge?\nmyArray.push('modifying', 'outside', 'does', 'not', 'purge');\npurge.length; // =\u003e 13 (over the maxlength!)\npurge.auto(); // =\u003e [1, 20, 'new value']\npurge.length; // =\u003e 3\n\npurge.clear();\npurge.value; // =\u003e []\npurge.length; // =\u003e 0\n```\n\n## API Documentation\n\n### Methods\n#### AutoPurge([maxlength], [array])\nConstructor function to create a new purgable array. Can be called with or without the `new` keyword.\n\nRequired | Argument      | Type      | Description\n-------- | ------------- | --------- | ------------\nNo       | maxlength     | Number    | The maxlength the array can be. Default is `50`.\nNo       | array         | Array     | An external array to use. Default is `[]`.\n\n```javascript\nvar purge = new AutoPurge();\nvar purge2 = AutoPurge(10);\nvar purge3 = AutoPurge(null, ['custom', 'array']);\nvar purge4 = new AutoPurge(10, ['custom', 'array']);\n```\n\nReturns `AutoPurge`\n\n#### AutoPurge.push(...)\nPush item(s) to the array. Each item should be an argument if pushing multiple items. If you need to push an array of items, use `purge.push.apply(purge, [1, 2, 3, 4, 5]);`.\n\nRequired | Argument      | Type      | Description\n-------- | ------------- | --------- | ------------\nYes      | ...           | Any       | Each item to push to the array as a separate argument.\n\n```javascript\npurge.push('new item');\npurge.push(1, 2, 'test', false);\npurge.push.apply(purge, [1, 2, 3, 4, 5]);\n```\n\nReturns `Number` (the number of items purged as result of the push)\n\n#### AutoPurge.purge([num])\nPurge the given number of old items from the array or remove all items without resetting the `_purged` counter like `AutoPurge.clear()`.\n\nRequired | Argument      | Type               | Description\n-------- | ------------- | ------------------ | ------------\nNo       | num           | Number/String/null | The number of items to remove, `null`, `undefined`, or `'all'` to remove all items.\n\n```javascript\nvar purge = new AutoPurge(10, ['custom', 'array', 1, 2, 3, 4]);\npurge.purge(2); // =\u003e ['custom', 'array']\n\npurge.purge(); // =\u003e [1, 2, 3, 4]\npurge.value; // =\u003e []\npurge._purged; // =\u003e 6\n```\n\nReturns `Array` (the items purged from the array)\n\n#### AutoPurge.auto()\nIf the array is modified without using any of the API methods, you will need to purge it. This will purge the array if the length of the array is greater than `maxlength`.\n\n```javascript\nvar arr = ['custom', 'array', 1, 2, 3, 4, true, false, {}, []],\n    purge = new AutoPurge(10, arr);\n\npurge.length; // =\u003e 10\narr.push('not', 'using', 'AutoPurge');\npurge.length; // =\u003e 13\n\npurge.auto(); // =\u003e ['custom', 'array', 1]\npurge.length; // =\u003e 13\n```\n\nReturns `Array` (the items purged from the array)\n\n#### AutoPurge.clear()\nClears the array and resets the `_purged` counter back to 0.\n\n```javascript\nvar purge = new AutoPurge(10, ['custom', 'array', 1, 2, 3, 4]);\npurge.purge(2); // =\u003e ['custom', 'array']\npurge._purged; // =\u003e 2\n\npurge.clear();\npurge.value; // =\u003e []\npurge._purged; // =\u003e 0\n```\n\nReturns `undefined`\n\n### Properties\n#### AutoPurge.length\n**Type:** Number (getter)\u003cbr\u003e\nThe total length of the array.\n\n#### AutoPurge.value\n**Type:** Array (getter)\u003cbr\u003e\nReturns the purgable array.\n\n#### AutoPurge.maxlength\n**Type:** Number\u003cbr\u003e\nThe configuration maxlength for the array.\n\n#### AutoPurge._store\n**Type:** Array\u003cbr\u003e\nReference to the purgable array.\n\n#### AutoPurge._purged\n**Type:** Number\u003cbr\u003e\nThe total items that have been purged from the array.\n\n---\n\n## TODO\n\n* Make the array observable when it's no longer \"hacky\" to do so.\n\n## Contributing\nWant to fix a bug or implement a new feature? Submit a pull request! Before you submit the request, please follow the guidelines below:\n\n* If you implement a new feature, write the test case for it.\n* Make sure all tests pass.\n* Build the minified version using grunt.\n* If bumping version, update `package.json`, `bower.json` and the JS file.\n\n## License\nLicensed under MIT.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkyleross%2Fautopurge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkyleross%2Fautopurge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkyleross%2Fautopurge/lists"}