Skip to content

Commit

Permalink
fix: use Map to preserve groups order (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
anjmao committed Apr 6, 2018
1 parent b93b25a commit aff8468
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
7 changes: 5 additions & 2 deletions release.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#!/bin/bash
git pull
# update changelog
cd ./src
node ../node_modules/standard-version/bin/cli.js --infile ../CHANGELOG.md
cd ..

# build lib
yarn run build

read -p "Check changelog and press enter to push tags" tags
# push tags
git push --follow-tags origin master

read -p "One more thing. Press enter to release to npm" npm
# push to npm
cp README.md ./dist
cd ./dist
yarn publish --access=public
32 changes: 20 additions & 12 deletions src/ng-select/items-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import * as searchHelper from './search-helper';
import { NgSelectComponent } from './ng-select.component';
import { isObject, isDefined } from './value-utils';

type OptionGroups = Map<string, NgOption[]>;

export class ItemsList {

private _items: NgOption[] = [];
private _filteredItems: NgOption[] = [];
private _groups: { [index: string]: NgOption[] };
private _groups: OptionGroups;
private _markedIndex = -1;
private _selected: NgOption[] = [];

Expand Down Expand Up @@ -47,7 +49,8 @@ export class ItemsList {
this._groups = this._groupBy(this._items, this._ngSelect.groupBy);
this._items = this._flatten(this._groups);
} else {
this._groups = { undefined: this._items };
this._groups = new Map();
this._groups.set(undefined, this._items)
}
this._filteredItems = [...this._items];
}
Expand Down Expand Up @@ -125,9 +128,9 @@ export class ItemsList {
term = this._ngSelect.searchFn ? term : searchHelper.stripSpecialChars(term).toLocaleLowerCase();
const match = this._ngSelect.searchFn || this._defaultSearchFn;

for (const key of Object.keys(this._groups)) {
for (const key of Array.from(this._groups.keys())) {
const matchedItems = [];
for (const item of this._groups[key]) {
for (const item of this._groups.get(key)) {
if (this._ngSelect.hideSelected && this._selected.indexOf(item) > -1) {
continue;
}
Expand Down Expand Up @@ -255,20 +258,25 @@ export class ItemsList {
return this._selected[this._selected.length - 1];
}

private _groupBy(items: NgOption[], prop: string | Function): { [index: string]: NgOption[] } {
private _groupBy(items: NgOption[], prop: string | Function): OptionGroups {
const isPropFn = prop instanceof Function;
const groups = items.reduce((grouped, item) => {
const key = isPropFn ? (<Function>prop).apply(this, [item.value]) : item.value[<string>prop];
grouped[key] = grouped[key] || [];
grouped[key].push(item);
const group = grouped.get(key);
if (group) {
group.push(item);
} else {
grouped.set(key, [item]);
}
return grouped;
}, {});
}, new Map<string, NgOption[]>());
return groups;
}

private _flatten(groups: { [index: string]: NgOption[] }) {
private _flatten(groups: OptionGroups) {
let i = 0;
return Object.keys(groups).reduce((items: NgOption[], key: string) => {

return Array.from(groups.keys()).reduce((items: NgOption[], key: string) => {
const parent: NgOption = {
label: key,
hasChildren: true,
Expand All @@ -278,9 +286,9 @@ export class ItemsList {
parent.value = {};
parent.value[this._ngSelect.groupBy] = key;
items.push(parent);
i++
i++;

const children = groups[key].map(x => {
const children = groups.get(key).map(x => {
x.parent = parent;
x.hasChildren = false;
i++;
Expand Down

0 comments on commit aff8468

Please sign in to comment.