valor-software / ng2-select

Angular based replacement for select boxes
http://valor-software.github.io/ng2-select/
MIT License
675 stars 587 forks source link

Item Id filter check #824

Open smasala opened 7 years ago

smasala commented 7 years ago

Are the null checks correct in select.ts ?

   this._items = value.filter((item:any) => {
        if ((typeof item === 'string') || (typeof item === 'object' && item && item[this.textField] && item[this.idField])) {
          return item;
        }
   });
   this.itemObjects = this._items.map((item:any) => (typeof item === 'string' ? new SelectItem(item) : new SelectItem({id: item[this.idField], text: item[this.textField], children: item[this.childrenField]})));

The check: && item[this.idField] means that the item property id cannot be "0", 0, undefined or null, or? Therefore if: items = [{text: "hello"}] or items = [{id: 0, "hello"}] would result in value.filter() always returning an empty array. new SelectItem() which checks the object and assigns the text value to the id property is then redundant as this step is never reached since this._items is empty.