Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style(eslint): apply specified code style changes #138

Merged
merged 27 commits into from
Oct 17, 2022
Merged
Changes from 24 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
63 changes: 32 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ const _hasOwn = Object.prototype.hasOwnProperty;
* @returns {T}
* @template T
*/
export function map<T>(opt_initial: T | undefined) {
const obj = Object.create(null);
export const map = <T>(opt_initial: T | undefined): object => {
const object = Object.create(null);
if (opt_initial) {
Object.assign(obj, opt_initial);
Object.assign(object, opt_initial);
}
return obj;

// FIXME(@DerekNonGeneric): Should we be creating objects w/ null protos?
return { ...opt_initial };
DerekNonGeneric marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -46,8 +48,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 const hasOwn = <T>(object: T, key: string): boolean => {
return _hasOwn.call(object, key);
}

/**
Expand All @@ -57,12 +59,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 const ownProperty = (
object: Record<string, number | RegExp>,
key: string,
): unknown => {
return hasOwn(object, key) ? Reflect.get(object, key) : undefined;
}

interface ITargetSourceDepth {
Expand All @@ -82,12 +83,12 @@ interface ITargetSourceDepth {
* @throws {Error} If source contains a circular reference.
* Note: Only nested objects are deep-merged, primitives and arrays are not.
*/
export function deepMerge(target: Object, source: Object, depth = 10): Object {
export const deepMerge = (target: object, source: object, depth = 10): object => {
// Keep track of seen objects to detect recursive references.
const seen: Array<Object> = [];
const seen: object[] = [];

/** @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 +105,19 @@ export function deepMerge(target: Object, source: Object, depth = 10): Object {
Object.assign(t, s);
continue;
}
Object.keys(s).forEach((key) => {
const newValue = s[key];
for (const key of Object.keys(s)) {
const newValue = Reflect.get(s, key);
// 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];
const oldValue = Reflect.get(t, key);
if (isObject(newValue) && isObject(oldValue)) {
queue.push({ t: oldValue, s: newValue, d: d + 1 });
return;
continue;
}
}
t[key] = newValue;
});
Reflect.set(t, key, newValue);
}
}
return target;
}
Expand All @@ -129,8 +130,8 @@ export function deepMerge(target: Object, source: Object, depth = 10): Object {
export function objectsEqualShallow(
DerekNonGeneric marked this conversation as resolved.
Show resolved Hide resolved
o1: Record<string, number | RegExp> | null | undefined,
o2: Record<string, number | RegExp> | null | undefined
): boolean {
if (o1 == null || o2 == null) {
): boolean => {
if (o1 == undefined || o2 == undefined) {
// Null is only equal to null, and undefined to undefined.
return o1 === o2;
}
Expand Down Expand Up @@ -159,15 +160,15 @@ export function objectsEqualShallow(
* @returns {R}
* @template T,R
*/
export function memo<T, P extends keyof T>(
obj: T,
prop: P,
factory: (arg0: T, arg1: P) => T[P]
): T[P] {
let result = obj[prop];
export const memo = <T, P extends keyof T>(
object: T,
property: P,
factory: (argument0: T, argument1: P) => T[P],
): T[P] => {
let result = Reflect.get(object, property);
if (result === undefined) {
result = factory(obj, prop);
obj[prop] = result;
result = factory(object, property);
Reflect.set(object, property, result);
}
return result;
}