Skip to content
Merged
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
54574c8
style(eslint): apply specified code style changes
ignoreintuition Oct 15, 2022
ebe562d
fix(src): use object spread
DerekNonGeneric Oct 16, 2022
c33049e
fix(src): use object literal instead of banned type
DerekNonGeneric Oct 16, 2022
c34389b
style(index): prefer tailing commas for js objects
DerekNonGeneric Oct 16, 2022
607e6c0
fix(src): update to use trailing commans for js objects
DerekNonGeneric Oct 16, 2022
24ced7d
fix
DerekNonGeneric Oct 16, 2022
3209b2a
Update src/index.ts
DerekNonGeneric Oct 16, 2022
ffc58ba
Update src/index.ts
DerekNonGeneric Oct 16, 2022
6da9d15
Update src/index.ts
DerekNonGeneric Oct 16, 2022
96e08c3
Update src/index.ts
DerekNonGeneric Oct 16, 2022
8e963d1
Update src/index.ts
DerekNonGeneric Oct 16, 2022
fcb48a3
Update src/index.ts
DerekNonGeneric Oct 16, 2022
5dce043
add note to return to later
DerekNonGeneric Oct 16, 2022
79f5d80
Update src/index.ts
DerekNonGeneric Oct 16, 2022
e66a051
Update src/index.ts
DerekNonGeneric Oct 16, 2022
5c7205c
Update src/index.ts
DerekNonGeneric Oct 16, 2022
74f22dc
Update src/index.ts
DerekNonGeneric Oct 17, 2022
d13b925
Update src/index.ts
DerekNonGeneric Oct 17, 2022
4545791
Update src/index.ts
DerekNonGeneric Oct 17, 2022
1b80afe
Update src/index.ts
DerekNonGeneric Oct 17, 2022
a14acd2
Update src/index.ts
DerekNonGeneric Oct 17, 2022
881139f
Update src/index.ts
DerekNonGeneric Oct 17, 2022
c2f6013
Update src/index.ts
DerekNonGeneric Oct 17, 2022
c077e4f
Update src/index.ts
DerekNonGeneric Oct 17, 2022
934c84e
Update src/index.ts
DerekNonGeneric Oct 17, 2022
b137820
Merge branch 'main' into style/ignoreintuition
DerekNonGeneric Oct 17, 2022
4fc93ee
fix(src): address remaining typecheck issues
DerekNonGeneric Oct 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ const _hasOwn = Object.prototype.hasOwnProperty;
* @template T
*/
export function map<T>(opt_initial: T | undefined) {
Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
const obj = Object.create(null);
const object = Object.create(null);
if (opt_initial) {
Object.assign(obj, opt_initial);
Object.assign(object, opt_initial);
}
return obj;
return { ...opt_initial };
Comment thread
DerekNonGeneric marked this conversation as resolved.
}

/**
Expand All @@ -46,8 +46,8 @@ export function map<T>(opt_initial: T | undefined) {
* @returns {boolean}
* @template T
*/
export function hasOwn<T>(obj: T, key: string) {
return _hasOwn.call(obj, key);
export function hasOwn<T>(object: T, key: string) {
Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
return _hasOwn.call(object, key);
}

/**
Expand All @@ -57,12 +57,11 @@ export function hasOwn<T>(obj: T, key: string) {
* @param {string} key
* @returns {unknown}
*/
export function ownProperty(obj: Record<string, number | RegExp>, key: string) {
if (hasOwn(obj, key)) {
return obj[key];
} else {
return undefined;
}
export function ownProperty(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

@DerekNonGeneric DerekNonGeneric Oct 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see https://mdn.io/relect-get

I believe the Reflect.get static method would also be able to access any inherited properties (e.g., from the base Object superclass — all the properties from the constructor would be included), which i think we'd like to avoid.

For security purposes, i believe only having access to all enumerable own properties would be safer and more efficient iiuc.

Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
object: Record<string, number | RegExp>,
key: string,
) {
Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
return hasOwn(object, key) ? object[key] : undefined;
Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
}

interface ITargetSourceDepth {
Expand All @@ -84,10 +83,10 @@ interface ITargetSourceDepth {
*/
export function deepMerge(target: Object, source: Object, depth = 10): Object {
Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
// Keep track of seen objects to detect recursive references.
const seen: Array<Object> = [];
const seen: Array<{}> = [];
Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated

/** @type {!Array<ITargetSourceDepth>} */
const queue: Array<ITargetSourceDepth> = [];
const queue: ITargetSourceDepth[] = [];
queue.push({ t: target, s: source, d: 0 });

// BFS to ensure objects don't have recursive references at shallower depths.
Expand All @@ -104,19 +103,19 @@ export function deepMerge(target: Object, source: Object, depth = 10): Object {
Object.assign(t, s);
continue;
}
Object.keys(s).forEach((key) => {
for (const key of Object.keys(s)) {
const newValue = s[key];
Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
// Perform a deep merge IFF both target and source have the same key
// whose corresponding values are objects.
if (hasOwn(t, key)) {
const oldValue = t[key];
Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
if (isObject(newValue) && isObject(oldValue)) {
queue.push({ t: oldValue, s: newValue, d: d + 1 });
return;
continue;
}
}
t[key] = newValue;
Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
});
}
}
return target;
}
Expand All @@ -130,7 +129,7 @@ export function objectsEqualShallow(
o1: Record<string, number | RegExp> | null | undefined,
o2: Record<string, number | RegExp> | null | undefined
): boolean {
Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
if (o1 == null || o2 == null) {
if (o1 == undefined || o2 == undefined) {
// Null is only equal to null, and undefined to undefined.
return o1 === o2;
}
Expand Down Expand Up @@ -160,14 +159,14 @@ export function objectsEqualShallow(
* @template T,R
*/
export function memo<T, P extends keyof T>(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, i knew about that proposal, but am not yet sure how it relates to this function. Is the difference that this is Object memo and that would be Function memo?

/cc @js-choi

Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
obj: T,
prop: P,
factory: (arg0: T, arg1: P) => T[P]
object: T,
property: P,
factory: (argument0: T, argument1: P) => T[P],
): T[P] {
Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
let result = obj[prop];
let result = object[property];
Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
if (result === undefined) {
result = factory(obj, prop);
obj[prop] = result;
result = factory(object, property);
object[property] = result;
Comment thread
DerekNonGeneric marked this conversation as resolved.
Outdated
}
return result;
}