feat: do not use Object.defineProperty?

This commit is contained in:
2026-01-17 16:52:58 +08:00
parent 09bfbca64a
commit 513b43965c
2 changed files with 8 additions and 33 deletions

View File

@@ -4,6 +4,7 @@
import type { NixValue, NixAttrs, NixList } from "../types";
import { forceAttrs, forceString, forceFunction, forceList } from "../type-assert";
import { createThunk } from "../thunk";
export const attrNames = (set: NixValue): string[] => Object.keys(forceAttrs(set)).sort();
@@ -22,17 +23,13 @@ export const hasAttr =
export const mapAttrs =
(f: NixValue) =>
(attrs: NixValue): NixAttrs => {
const forced_attrs = forceAttrs(attrs);
const forced_f = forceFunction(f);
const new_attrs: NixAttrs = {};
for (const key in forced_attrs) {
Object.defineProperty(new_attrs, key, {
get: () => forceFunction(forced_f(key))(forced_attrs[key]),
enumerable: true,
configurable: true,
});
const forcedAttrs = forceAttrs(attrs);
const forcedF = forceFunction(f);
const newAttrs: NixAttrs = {};
for (const key in forcedAttrs) {
newAttrs[key] = createThunk(() => forceFunction(forcedF(key))(forcedAttrs[key]));
}
return new_attrs;
return newAttrs;
};
export const removeAttrs =

View File

@@ -250,27 +250,5 @@ export const op = {
return Array.prototype.concat.call(forceList(a), forceList(b));
},
update: (a: NixValue, b: NixValue): NixAttrs => {
const forcedA = forceAttrs(a);
const forcedB = forceAttrs(b);
const newAttrs: NixAttrs = {};
for (const key in forcedA) {
Object.defineProperty(newAttrs, key, {
get: () => force(forcedA[key]),
enumerable: true,
configurable: true,
});
}
for (const key in forcedB) {
Object.defineProperty(newAttrs, key, {
get: () => force(forcedB[key]),
enumerable: true,
configurable: true,
});
}
return newAttrs;
},
update: (a: NixValue, b: NixValue): NixAttrs => ({ ...forceAttrs(a), ...forceAttrs(b) }),
};