UNPKG

@oarepo/oarepo-ui-vue

Version:
77 lines (64 loc) 2.04 kB
export default class Query { constructor(queryDict) { this.query = Object.assign({}, queryDict || {}); } set(key, value) { const stringValue = value.toString(); const prev = this.query[key]; if (prev === undefined) { this.query[key] = []; } else if (!Array.isArray(this.query[key])) { this.query[key] = [this.query[key]]; } else { this.query[key] = this.query[key].slice(); } this.query[key] = this.query[key].filter(x => x !== stringValue); this.query[key].push(stringValue); } remove(key, value) { const stringValue = value.toString(); const prev = this.query[key]; if (prev === undefined) { return; } if (!Array.isArray(this.query[key])) { this.query[key] = [this.query[key]]; } else { this.query[key] = this.query[key].slice(); } this.query[key] = this.query[key].filter(x => x !== stringValue); if (this.query[key].length === 0) { delete this.query[key]; } } removeKey(key) { if (this.query[key] !== undefined) { delete this.query[key]; } } replace(key, value) { this.removeKey(key); this.set(key, value); } has(key, value) { const stringValue = value.toString(); const prev = this.query[key]; if (prev === undefined) { return false; } if (!Array.isArray(this.query[key])) { this.query[key] = [this.query[key]]; } return this.query[key].filter(x => x === stringValue).length > 0; } get(key, singleValue = false) { const val = this.query[key]; if (val === undefined || val === []) { return singleValue ? undefined : []; } if (!Array.isArray(this.query[key])) { return singleValue ? val : [val]; } return singleValue ? val[0] : val; } }