Skip to content

Instantly share code, notes, and snippets.

@DrMabuse23
Last active November 7, 2016 23:56
Show Gist options
  • Save DrMabuse23/7ec113b11b2b004ca52c17585ade1b71 to your computer and use it in GitHub Desktop.
Save DrMabuse23/7ec113b11b2b004ca52c17585ade1b71 to your computer and use it in GitHub Desktop.
import { Events } from 'ionic-angular';
import { Output, EventEmitter } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/Rx';
/**
*
* @example {"artist":{"name":"The Pandoras"},"album":"Stop Pretending","rating":0,"ownerShip":"Vinyl","tags":""}
* @export
* @interface IEntry
*/
export interface IEntry {
/**
* @type {{
* name: string
* }}
* @memberOf IEntry
*/
artist: {
name: string
},
/**
* @type {string}
* @memberOf IEntry
*/
album: string,
/**
* @type {number}
* @memberOf IEntry
*/
rating: number,
/**
* @type {string}
* @memberOf IEntry
*/
ownerShip: string,
/**
* @type {*}
* @memberOf IEntry
*/
tags: any,
/**
* @type {number}
* @memberOf IEntry
*/
charAt: number
}
const INFO = 'info';
@Injectable()
export class Store {
public _artists: any;
private _resp: any = undefined;
constructor(private http: Http, public events: Events) {}
/**
*
* @returns {Observable<Array<IEntry>>}
*
* @memberOf Store
*/
import(): Observable<Array<IEntry>> {
this.events.publish('log', { type: INFO, msg: 'start import' });
return this.http.get('./assets/rawlist.json')
.map((resp) => {
this.events.publish('log', { type: INFO, msg: 'parse json' });
return resp.json();
});
}
/**
*
* @param {string} [sortType='sortAlbum']
* @returns {Observable<IEntry>}
*
* @memberOf Store
*/
importWithSortByType(sortType: string = 'sortAlbum'): Observable<IEntry> {
return this.import().exhaustMap((result) => {
this._resp = result;
this.events.publish('log', { type: INFO, msg: 'GET Result' });
const temp = this[sortType]();
return Observable.from(temp);
});
}
/**
*
* @returns {Array<IEntry>}
*
* @memberOf Store
*/
sortArtist(): Array<IEntry> {
const sorted = this._resp.sort((a, b) => {
if (a.artist.name < b.artist.name) {
return -1;
}
if (a.artist.name > b.artist.name) {
return 1;
}
return 0;
});
this.events.publish('log', { type: INFO, msg: 'artist is sorted' });
return sorted;
}
/**
*
* @returns {Array<IEntry>}
*
* @memberOf Store
*/
sortAlbum(): Array<IEntry> {
const sorted = this._resp.sort((a, b) => {
const indexA = a.album.search(/[a-zA-Z]/);
const indexB = b.album.search(/[a-zA-Z]/);
if (a.album.charAt(indexA) < b.album.charAt(indexB)) {
return -1;
}
if (a.album.charAt(indexA) > b.album.charAt(indexB)) {
return 1;
}
return 0;
});
return sorted;
}
/**
*
* @param {boolean} [artist=false]
* @returns {Observable<IEntry>}
*
* @memberOf Store
*/
getData(artist: boolean = false): Observable<IEntry> {
let sortType: string = 'sortAlbum';
if (!this._resp) {
if (artist) {
sortType = 'sortArtist';
}
return this.importWithSortByType(sortType);
}
if (artist) {
sortType = 'sortArtist';
}
const temp = this[sortType]();
return Observable.from(temp);
}
/**
*
* @returns {Observable<{ group: string, qt: Array<IEntry> }>}
*
* @memberOf Store
*/
artists(): Observable<{ group: string, qt: Array<IEntry> }> {
return this.getData(true)
.groupBy((x) => {
return x.artist.name.trim();
})
.flatMap(group => {
return group.reduce((acc, currentValue) => {
acc.group = currentValue.artist.name.trim();
if (!acc.qt) {
acc.qt = [];
}
currentValue.charAt = -1;
acc.qt.push(currentValue);
return acc;
}, { group: undefined, qt: [] });
});
}
/**
*
* @returns {Observable<{ group: string, qt: Array<IEntry> }>}
*
* @memberOf Store
*/
abc(): Observable<{ group: string, qt: Array<IEntry> }> {
return this.getData()
.groupBy((x) => {
const index = x.album.search(/[a-zA-Z]/);
return x.album.charAt(index).toLowerCase();
})
.flatMap(group => {
return group.reduce((acc, currentValue) => {
const ind = currentValue.album.search(/[a-zA-Z]/);
const key = currentValue.album.charAt(ind).toLowerCase();
acc.group = key;
if (!acc.qt) {
acc.qt = [];
}
currentValue.charAt = ind;
acc.qt.push(currentValue);
return acc;
}, { group: undefined, qt: [] });
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment