-
Notifications
You must be signed in to change notification settings - Fork 292
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
ES6: migrated "util" #1114
Merged
ES6: migrated "util" #1114
Changes from 31 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
f5fa8c2
worker
bennycode 518df38
[ci skip]
bennycode e0fbb2e
wording
bennycode 289acda
[ci skip]
bennycode 49684cd
[ci skip]
bennycode f9c7ab8
[ci skip]
bennycode a974bca
[ci skip]
bennycode b533fad
confirm
bennycode a605cf7
[ci skip]
bennycode f89513a
[ci skip]
bennycode a5e1ab7
es6
bennycode f2fef67
DebugUtil
bennycode f12cef4
Linting
bennycode dee0d7e
z.util.KEYCODE
bennycode c8ad04c
KeyUtil
bennycode 6079fe7
LocalizerUtil
bennycode ea6e8af
marked
bennycode 96766a5
moment
bennycode 32b1554
NumberUtil
bennycode 7ee62c3
tests pass
bennycode 519b8b2
protobuf
bennycode 5a19a52
scroll-helpers
bennycode ce40405
Statistics
bennycode 6aa721f
Statistics
bennycode 89a37e6
StorageUtil
bennycode 205cb5b
StringUtil
bennycode eb12f4b
No Array.from
bennycode 6614a5f
util
bennycode 9466e7f
tests pass
bennycode 3f883aa
JSDoc declaration for optional
bennycode 8d6d0ec
Login fix
bennycode b440c39
Merge branch 'dev' into es6/util
bennycode f52d0ce
[ci skip]
bennycode 18ca135
[ci skip]
bennycode 1836115
[ci skip]
bennycode 3f1fe58
[ci skip]
bennycode 78026f8
[ci skip]
bennycode 28edc65
[ci skip]
bennycode 0085ca8
[ci skip]
bennycode b969b3a
[ci skip]
bennycode 81031a1
[ci skip]
bennycode 2504a22
[ci skip]
bennycode 16c99d2
[ci skip]
bennycode 27a8f4b
[ci skip]
bennycode 16bc8d3
[ci skip]
bennycode e03b7f1
[ci skip]
bennycode b42458d
[ci skip]
bennycode 8adf5ec
fixed indices
bennycode 4937ef6
Added filter check
bennycode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2017 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
'use strict'; | ||
|
||
window.z = window.z || {}; | ||
window.z.util = z.util || {}; | ||
|
||
z.util.ArrayUtil = { | ||
chunk(array, size) { | ||
const chunks = []; | ||
const temp_array = Array.from(array); | ||
while (temp_array.length) { | ||
chunks.push(temp_array.splice(0, size)); | ||
} | ||
return chunks; | ||
}, | ||
find_closest(array, value) { | ||
let closest = array[0]; | ||
|
||
array.forEach(function(current) { | ||
if (value >= current) { | ||
closest = current; | ||
} | ||
}); | ||
|
||
return closest; | ||
}, | ||
get_next_item(array, item, filter) { | ||
const index = array.indexOf(item); | ||
const next_index = index + 1; | ||
|
||
// couldn't find the item | ||
if (index === -1) { | ||
return null; | ||
} | ||
|
||
// item is last item in the array | ||
if ((next_index === array.length) && (index > 0)) { | ||
return array[index - 1]; | ||
} | ||
|
||
if (next_index >= array.length) { | ||
return undefined; | ||
} | ||
|
||
for (let i = next_index; i <= array.length; i++) { | ||
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. You have a proper array. We can use that:
|
||
const current_item = array[i]; | ||
if ((filter == null) || !!filter(current_item)) { | ||
return current_item; | ||
} | ||
} | ||
}, | ||
/** | ||
* Interpolates an array of numbers using linear interpolation | ||
* | ||
* @param {Array<any>} array - source | ||
* @param {number} length - new length | ||
* @returns {Array<any>} new array with interpolated values | ||
*/ | ||
interpolate(array, length) { | ||
const new_array = []; | ||
const scale_factor = (array.length - 1) / (length - 1); | ||
|
||
new_array[0] = array[0]; | ||
new_array[length - 1] = array[array.length - 1]; | ||
|
||
for (let i = 1; i < length - 1; i++) { | ||
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.
|
||
const original_index = i * scale_factor; | ||
const before = Math.floor(original_index).toFixed(); | ||
const after = Math.ceil(original_index).toFixed(); | ||
const point = original_index - before; | ||
new_array[i] = array[before] + ((array[after] - array[before]) * point); // linear interpolation | ||
} | ||
|
||
return new_array; | ||
}, | ||
is_last_item(array, item) { | ||
return array.indexOf(item) === (array.length - 1); | ||
}, | ||
iterate_index(array, current_index) { | ||
if (!_.isArray(array) || !_.isNumber(current_index)) { | ||
return undefined; | ||
} | ||
|
||
if (!array.length) { | ||
return undefined; | ||
} | ||
|
||
return (current_index + 1) % array.length; | ||
}, | ||
/** | ||
* Returns random element | ||
* @param {Array} array - source | ||
* @returns {Object} random element | ||
*/ | ||
random_element(array = []) { | ||
return array[Math.floor(Math.random() * array.length)]; | ||
}, | ||
/** | ||
* Remove given element from array | ||
* @param {Array} array - source | ||
* @param {Object} element - Element which should be removed | ||
* @returns {Array|undefined} containing the removed element | ||
*/ | ||
remove_element(array = [], element) { | ||
const index = array.indexOf(element); | ||
if (index > -1) { | ||
return array.splice(index, 1); | ||
} | ||
}, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Problematic. If error is a string as it sometimes is and not the error object, we will end up with a log message that does no longer contain the "Caused by reference". Maybe we should be more explicit and do a proper _.isObject check with tow error logging cases.
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.
Hm, this is due to our logger implementation, right?