Skip to content
This repository has been archived by the owner on Jan 21, 2021. It is now read-only.

Commit

Permalink
accessors v2: electric boolagoo
Browse files Browse the repository at this point in the history
  • Loading branch information
hb432 committed Jun 15, 2020
1 parent 6f0a8db commit e9dcb4f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ Converts the input `string` into a [UUID](https://docs.oracle.com/javase/10/docs
### `framework.values(object)`
An alias for `Object.values(object)`.

### `framework.wrapper(object)`
Adds nashorn-style accessors to the input `object`. These accessors themselves implement `framework.wrapper`, meaning any accessed property will also have accessors, and so on.

# Polyfills
### `atob(), btoa()`
```javascript
Expand Down
48 changes: 48 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,54 @@
},
values: (object) => {
return Object.values(object);
},
wrapper: (object) => {
if (object === null || typeof object !== 'object') {
return object;
} else {
const output = { instance: object };
framework.entries(object).forEach((entry) => {
let index = undefined;
entry.key.startsWith('is') && entry.key[2] && (index = 2);
entry.key.startsWith('get') && entry.key[3] && (index = 3);
if (index) {
let key = entry.key.slice(index);
if (key.length) {
let camel = framework.lower(key[0]) + key.slice(1);
if (!framework.keys(object).includes(camel)) {
try {
entry.value();
Object.defineProperty(output, camel, {
get () {
return framework.wrapper(entry.value());
},
set (value) {
return object[`set${key}`] && object[`set${key}`](value);
}
});
} catch (error) {}
}
}
} else {
Object.defineProperty(output, entry.key, {
get () {
return framework.wrapper(entry.value);
}
});
}
});
const array = framework.array(object);
framework.keys(array).forEach((index) => {
if (!framework.keys(output).includes(index)) {
Object.defineProperty(output, index, {
get () {
return framework.wrapper(array[index]);
}
});
}
});
return output;
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e9dcb4f

Please sign in to comment.