{"id":19041951,"url":"https://github.com/writetome51/object-in-session-storage","last_synced_at":"2026-05-17T19:04:00.298Z","repository":{"id":122062445,"uuid":"188913876","full_name":"writetome51/object-in-session-storage","owner":"writetome51","description":"A TypeScript/JavaScript class representing an object or array stored in the browser's sessionStorage","archived":false,"fork":false,"pushed_at":"2019-05-27T21:59:34.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-14T10:04:35.309Z","etag":null,"topics":["browser","javascript","object","session","session-storage","sessionstorage","storage","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/writetome51.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-27T21:59:22.000Z","updated_at":"2019-05-27T22:25:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"f2536c2e-5078-402c-9392-60ddd588566e","html_url":"https://github.com/writetome51/object-in-session-storage","commit_stats":{"total_commits":2,"total_committers":1,"mean_commits":2.0,"dds":0.0,"last_synced_commit":"ec9afa8311ef22ab32c8324ee6cb0fc683071380"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/writetome51%2Fobject-in-session-storage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/writetome51%2Fobject-in-session-storage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/writetome51%2Fobject-in-session-storage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/writetome51%2Fobject-in-session-storage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/writetome51","download_url":"https://codeload.github.com/writetome51/object-in-session-storage/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240100512,"owners_count":19747683,"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":["browser","javascript","object","session","session-storage","sessionstorage","storage","typescript"],"created_at":"2024-11-08T22:33:29.709Z","updated_at":"2026-05-08T19:30:16.049Z","avatar_url":"https://github.com/writetome51.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ObjectInSessionStorage\n\nA TypeScript/JavaScript class representing an object or array stored in the  \nbrowser's `sessionStorage`. The item is identified by a unique string `this.key`  \nand stored as a `key:value` pair.   \nWhen you call the constructor, if the `key` argument is a string that isn't  \nempty and the `value` argument is not undefined or null, the item will be  \nstored immediately. Else, the item won't be stored until you call  \n`this.set(value)`.\n\nNote: this only works when run in a browser environment.\n\n\n## Constructor\n\n\u003cdetails\u003e\n\u003csummary\u003eview constructor\u003c/summary\u003e\n\n```ts\nconstructor(\n    key? = '',\n        // assigned to this.key\n\n    value?: Object | any[]  = undefined\n)\n    // If `key` is not an empty string and `value` is defined, the item \n    // is stored immediately.\n```\n\u003c/details\u003e\n\n\n## Properties\n\u003cdetails\u003e\n\u003csummary\u003eview properties\u003c/summary\u003e\n\n```ts\nkey: string // the unique ID for the stored object or array.\n    \nclassName: string // read-only\n```\n\u003c/details\u003e\n\n\n## Methods\n\u003cdetails\u003e\n\u003csummary\u003eview methods\u003c/summary\u003e\n\n```ts\nset(value: Object | any[]): void\n    // Saves item `value` in storage.  Replaces previous value, if any.\n\nget(): Object | any[]\n    // Returns the stored object or array.\n\ngetAsJSON(): string\n    // Returns stored object or array as JSON.\n\nmodify(changes: Object | any[]): void\n    // `changes` does not replace the current value.  It is merged into the current value.\n\nremove(): void\n    // After calling this, both the key and value are no longer in\n    // storage.  You can store the item again by calling this.set(value)\n```\nThe methods below are not important to know about in order to use this  \nclass.  They're inherited from [BaseClass](https://github.com/writetome51/typescript-base-class#baseclass) .\n```ts\nprotected   _createGetterAndOrSetterForEach(\n\t\tpropertyNames: string[],\n\t\tconfiguration: IGetterSetterConfiguration\n\t   ) : void\n    /*********************\n    Use this method when you have a bunch of properties that need getter and/or \n    setter functions that all do the same thing. You pass in an array of string \n    names of those properties, and the method attaches the same getter and/or \n    setter function to each property.\n    IGetterSetterConfiguration is this object:\n    {\n        get_setterFunction?: (\n             propertyName: string, index?: number, propertyNames?: string[]\n        ) =\u003e Function,\n\t    // get_setterFunction takes the property name as first argument and \n\t    // returns the setter function.  The setter function must take one \n\t    // parameter and return void.\n\t    \n        get_getterFunction?: (\n             propertyName: string, index?: number, propertyNames?: string[]\n        ) =\u003e Function\n\t    // get_getterFunction takes the property name as first argument and \n\t    // returns the getter function.  The getter function must return something.\n    }\n    *********************/ \n\t   \n\t   \nprotected   _returnThis_after(voidExpression: any) : this\n    // voidExpression is executed, then function returns this.\n    // Even if voidExpression returns something, the returned data isn't used.\n\n\nprotected   _errorIfPropertyHasNoValue(\n                property: string, // can contain dot-notation, i.e., 'property.subproperty'\n                propertyNameInError? = ''\n            ) : void\n    // If value of this[property] is undefined or null, it triggers fatal error:\n    // `The property \"${propertyNameInError}\" has no value.`\n```\n\u003c/details\u003e\n\n\n## Usage Example\n\u003cdetails\u003e\n\u003csummary\u003eview example\u003c/summary\u003e\n\n```ts\n// It might be a good idea to name each class instance after its key.\n// After instantiation, you wouldn't modify its `key` property.\n\nlet user1 = new ObjectInSessionStorage(\n    'user1',\n    {username: 'papasmurf', password: 'i_love_smurfette'}\n);\n\nlet user2 = new ObjectInSessionStorage(\n    'user2',\n    {username: 'smurfette', password: 'i_love_papa'}\n);\n\n// Or, you could create a singleton instance to handle all stored objects,\n// and change its `key` when you want to change what specific object to handle.\n\nlet objInSessionStorage = new ObjectInSessionStorage();\nobjInSessionStorage.key = 'user1';\nobjInSessionStorage.set({username: 'papasmurf', password: 'i_love_smurfette'});\n\nobjInSessionStorage.key = 'user2';\nobjInSessionStorage.set({username: 'smurfette', password: 'i_love_papa'});\n```\n\u003c/details\u003e\n\n\n## Inheritance Chain\n\nObjectInSessionStorage\u003c--[ObjectInBrowserStorage](https://github.com/writetome51/object-in-browser-storage#objectinbrowserstorage)\u003c--[ItemInBrowserStorage](https://github.com/writetome51/item-in-browser-storage#iteminbrowserstorage)\u003c--[BaseClass](https://github.com/writetome51/typescript-base-class#baseclass)\n\n\n## Installation\n\n```bash\nnpm i  @writetome51/object-in-session-storage\n```\n\n## Loading\n```ts\n// If using TypeScript:\nimport {ObjectInSessionStorage} from '@writetome51/object-in-session-storage';\n// If using ES5 JavaScript:\nvar ObjectInSessionStorage = \n    require('@writetome51/object-in-session-storage').ObjectInSessionStorage;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwritetome51%2Fobject-in-session-storage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwritetome51%2Fobject-in-session-storage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwritetome51%2Fobject-in-session-storage/lists"}