-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Group-One-Technology/jr.multiple-select
Feature: Multiple Select
- Loading branch information
Showing
6 changed files
with
597 additions
and
167 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
export default class MiniArray extends Array { | ||
constructor(...args) { | ||
super(...args); | ||
} | ||
|
||
get first() { | ||
return this[0]; | ||
} | ||
|
||
get last() { | ||
return this.at(-1); | ||
} | ||
|
||
nextOf(item) { | ||
const nextIndex = this.indexOf(item) + 1; | ||
return nextIndex >= this.length ? this.first : this.at(nextIndex); | ||
} | ||
|
||
previousOf(item) { | ||
const previousIndex = this.indexOf(item) - 1; | ||
return previousIndex < 0 ? this.last : this.at(previousIndex); | ||
} | ||
|
||
toggle(value) { | ||
let newValue; | ||
|
||
if (Array.isArray(value)) { | ||
newValue = value; | ||
} else { | ||
let index = this.indexOf(value); | ||
if (index === -1) { | ||
newValue = this.concat(value); | ||
} else { | ||
newValue = this.slice(0, index).concat(this.slice(index + 1)); | ||
} | ||
} | ||
|
||
return new MiniArray(...newValue); | ||
} | ||
|
||
add(value) { | ||
let index = this.indexOf(value); | ||
if (index !== -1) return this; | ||
|
||
const newValue = this.concat(value); | ||
|
||
return new MiniArray(...newValue); | ||
} | ||
|
||
remove(value) { | ||
let index = this.indexOf(value); | ||
if (index === -1) return this; | ||
|
||
const newValue = this.slice(0, index).concat(this.slice(index + 1)); | ||
|
||
return new MiniArray(...newValue); | ||
} | ||
|
||
subtract(arr) { | ||
return new MiniArray(...this.filter(item => !arr.includes(item))); | ||
} | ||
|
||
search(query) { | ||
const normalizedQuery = query.toLowerCase().split(/\s+/); | ||
|
||
const matches = this.filter(item => { | ||
const lowercaseItem = item.toLowerCase(); | ||
return normalizedQuery.every(word => lowercaseItem.includes(word)); | ||
}); | ||
|
||
return new MiniArray(...matches); | ||
} | ||
} |
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
Oops, something went wrong.