https://github.com/mmiller42/poproxy
ECMAScript Proxy factory for preserving insertion order of object keys
https://github.com/mmiller42/poproxy
Last synced: 3 months ago
JSON representation
ECMAScript Proxy factory for preserving insertion order of object keys
- Host: GitHub
- URL: https://github.com/mmiller42/poproxy
- Owner: mmiller42
- License: mit
- Created: 2021-06-14T18:54:44.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-06-16T06:47:34.000Z (about 4 years ago)
- Last Synced: 2025-03-11T01:09:58.608Z (3 months ago)
- Language: TypeScript
- Size: 39.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# poproxy
> Preserved Order Proxy
poproxy is a [`Proxy`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) constructor that preserves the original order of properties as they are assigned to the object, ignoring the [exceptions that apply to numbers and symbols](https://2ality.com/2015/10/property-traversal-order-es6.html).
## Example
```ts
import { preserveOrder } from "poproxy";interface Foo {
a: string;
b: string;
"0": string;
"1": string;
}const foo: Partial = {};
const proxy = preserveOrder(foo);proxy.b = "b";
proxy[1] = "1";
proxy["0"] = "0";
proxy.a = "a";Object.keys(foo); // ["0", "1", "b", "a"]
JSON.stringify(foo); // {"0":"0","1":"1","b":"b","a":"a"}Object.keys(proxy); // ["b", "1", "0", "a"]
JSON.stringify(proxy); // {"b":"b","1":"1","0":"0","a":"a"}
```Just like normal objects, if a property is deleted and then assigned, it will be appended rather than returning to its previous position:
```ts
delete proxy.b;
proxy.b = "B";Object.keys(foo); // ["0", "1", "a", "b"]
JSON.stringify(foo); // {"0":"0","1":"1","a":"a","b":"B"}Object.keys(proxy); // ["1", "0", "a", "b"]
JSON.stringify(proxy); // {"1":"1","0":"0","a":"a","b":"B"}
```