-
Notifications
You must be signed in to change notification settings - Fork 113
Develop #87
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
base: master
Are you sure you want to change the base?
Develop #87
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,16 +7,61 @@ | |
| * @returns {*} | ||
| */ | ||
| function reduce(callback, startValue) { | ||
| if (typeof callback !== 'function') { | ||
| throw new TypeError('Callback is not a function'); | ||
| } | ||
|
|
||
| const copyOfThis = { ...this }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 14: Critical issue — |
||
| const isArrayLike = this instanceof Object | ||
| && this.hasOwnProperty('length') | ||
| && typeof this.length === 'number' | ||
| && !Array.isArray(this); | ||
|
Comment on lines
+15
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 15–18: The |
||
|
|
||
| Object.defineProperty(copyOfThis, 'length', { | ||
| value: this.length, | ||
| enumerable: false, | ||
| }); | ||
|
|
||
| const len = copyOfThis.length >>> 0; | ||
|
Comment on lines
+20
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 20–25: You define |
||
| const hasStartValue = arguments.length >= 2; | ||
|
|
||
| if (len < 1 && !hasStartValue) { | ||
| // eslint-disable-next-line max-len | ||
| throw new TypeError(`Array is empty and doesn't have 'startValue' argument`); | ||
| } | ||
|
Comment on lines
+28
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 28–31: The check |
||
|
|
||
| let prev = startValue; | ||
| let startIndex = 0; | ||
| let start = 0; | ||
|
|
||
| if (arguments.length < 2) { | ||
| startIndex = 1; | ||
| prev = this[0]; | ||
| if (!hasStartValue) { | ||
| for (let i = 0; i < len; i++) { | ||
| if (i in copyOfThis) { | ||
| prev = copyOfThis[i]; | ||
| start = i + 1; | ||
| break; | ||
| } else if (i === len) { | ||
| throw new TypeError(`Index hasn't been founded`); | ||
| } | ||
| } | ||
|
Comment on lines
+36
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 36–45: The no-initial-value branch scans |
||
| } | ||
|
|
||
| for (let i = startIndex; i < this.length; i++) { | ||
| prev = callback(prev, this[i], i, this); | ||
| const copyOfThisArr = Object.assign([], copyOfThis); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 48: |
||
|
|
||
| for (let i = start; i < len; i++) { | ||
| if (!isArrayLike) { | ||
| if (!(i in copyOfThisArr)) { | ||
| continue; | ||
| } | ||
|
|
||
| prev = callback(prev, copyOfThisArr[i], i, copyOfThisArr); | ||
| continue; | ||
|
Comment on lines
+50
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 50–57: In the main loop you use |
||
| } | ||
|
|
||
| if (!(i in copyOfThis)) { | ||
| continue; | ||
| } | ||
|
|
||
| prev = callback(prev, copyOfThis[i], i, copyOfThis); | ||
|
Comment on lines
+60
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 60–64: The array-like branch similarly checks |
||
| } | ||
|
|
||
| return prev; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 10–12: Good — the early
typeof callback !== 'function'guard is present and correctly throws a TypeError. This satisfies the requirement that a non-function callback causes a TypeError (checklist 3.4).