MCPcopy Create free account
hub / github.com/immutable-js/immutable-js / hashJSObj

Function hashJSObj

src/Hash.ts:136–209  ·  view source on GitHub ↗
(obj: object | Function)

Source from the content-addressed store, hash-verified

134
135// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
136function hashJSObj(obj: object | Function): number {
137 let hashed: number | undefined;
138 if (usingWeakMap) {
139 // @ts-expect-error weakMap is defined
140 hashed = weakMap.get(obj);
141 if (hashed !== undefined) {
142 return hashed;
143 }
144 }
145
146 // @ts-expect-error used for old code, will be removed
147 hashed = obj[UID_HASH_KEY];
148 if (hashed !== undefined) {
149 return hashed;
150 }
151
152 if (!canDefineProperty) {
153 // @ts-expect-error used for old code, will be removed
154 hashed = obj.propertyIsEnumerable && obj.propertyIsEnumerable[UID_HASH_KEY];
155 if (hashed !== undefined) {
156 return hashed;
157 }
158
159 hashed = getIENodeHash(obj);
160 if (hashed !== undefined) {
161 return hashed;
162 }
163 }
164
165 hashed = nextHash();
166
167 if (usingWeakMap) {
168 // @ts-expect-error weakMap is defined
169 weakMap.set(obj, hashed);
170 } else if (isExtensible !== undefined && isExtensible(obj) === false) {
171 throw new Error('Non-extensible objects are not allowed as keys.');
172 } else if (canDefineProperty) {
173 Object.defineProperty(obj, UID_HASH_KEY, {
174 enumerable: false,
175 configurable: false,
176 writable: false,
177 value: hashed,
178 });
179 } else if (
180 obj.propertyIsEnumerable !== undefined &&
181 obj.propertyIsEnumerable === obj.constructor.prototype.propertyIsEnumerable
182 ) {
183 // Since we can't define a non-enumerable property on the object
184 // we'll hijack one of the less-used non-enumerable properties to
185 // save our hash on it. Since this is a function it will not show up in
186 // `JSON.stringify` which is what we want.
187 obj.propertyIsEnumerable = function () {
188 return this.constructor.prototype.propertyIsEnumerable.apply(
189 this,
190 // eslint-disable-next-line prefer-rest-params
191 arguments
192 );
193 };

Callers 1

hashFunction · 0.85

Calls 4

getIENodeHashFunction · 0.85
nextHashFunction · 0.85
getMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected