mirror of https://github.com/iptv-org/iptv
Install @freearhey/core
parent
03208a262a
commit
4c92b4ecb1
@ -1,175 +0,0 @@
|
||||
import _ from 'lodash'
|
||||
import { orderBy, Order } from 'natural-orderby'
|
||||
import { Dictionary } from './'
|
||||
|
||||
type Iteratee = (value: any, value2?: any) => void
|
||||
|
||||
export class Collection {
|
||||
_items: any[]
|
||||
|
||||
constructor(items?: any[]) {
|
||||
this._items = Array.isArray(items) ? items : []
|
||||
}
|
||||
|
||||
first(predicate?: Iteratee) {
|
||||
if (predicate) {
|
||||
return this._items.find(predicate)
|
||||
}
|
||||
|
||||
return this._items[0]
|
||||
}
|
||||
|
||||
last(predicate?: Iteratee) {
|
||||
if (predicate) {
|
||||
return _.findLast(this._items, predicate)
|
||||
}
|
||||
|
||||
return this._items[this._items.length - 1]
|
||||
}
|
||||
|
||||
find(iteratee: Iteratee): Collection {
|
||||
const found = this._items.filter(iteratee)
|
||||
|
||||
return new Collection(found)
|
||||
}
|
||||
|
||||
add(data: any) {
|
||||
this._items.push(data)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
intersects(collection: Collection): boolean {
|
||||
return _.intersection(this._items, collection.all()).length > 0
|
||||
}
|
||||
|
||||
count() {
|
||||
return this._items.length
|
||||
}
|
||||
|
||||
join(separator: string) {
|
||||
return this._items.join(separator)
|
||||
}
|
||||
|
||||
indexOf(value: string) {
|
||||
return this._items.indexOf(value)
|
||||
}
|
||||
|
||||
push(data: any) {
|
||||
this.add(data)
|
||||
}
|
||||
|
||||
uniq() {
|
||||
const items = _.uniq(this._items)
|
||||
|
||||
return new Collection(items)
|
||||
}
|
||||
|
||||
reduce(iteratee: Iteratee, accumulator: any) {
|
||||
const items = _.reduce(this._items, iteratee, accumulator)
|
||||
|
||||
return new Collection(items)
|
||||
}
|
||||
|
||||
filter(iteratee: Iteratee) {
|
||||
const items = _.filter(this._items, iteratee)
|
||||
|
||||
return new Collection(items)
|
||||
}
|
||||
|
||||
forEach(iteratee: Iteratee) {
|
||||
for (let item of this._items) {
|
||||
iteratee(item)
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
remove(iteratee: Iteratee): Collection {
|
||||
const removed = _.remove(this._items, iteratee)
|
||||
|
||||
return new Collection(removed)
|
||||
}
|
||||
|
||||
concat(collection: Collection) {
|
||||
const items = this._items.concat(collection._items)
|
||||
|
||||
return new Collection(items)
|
||||
}
|
||||
|
||||
isEmpty(): boolean {
|
||||
return this._items.length === 0
|
||||
}
|
||||
|
||||
notEmpty(): boolean {
|
||||
return this._items.length > 0
|
||||
}
|
||||
|
||||
sort() {
|
||||
const items = this._items.sort()
|
||||
|
||||
return new Collection(items)
|
||||
}
|
||||
|
||||
orderBy(iteratees: Iteratee | Iteratee[], orders?: Order | Order[]) {
|
||||
const items = orderBy(this._items, iteratees, orders)
|
||||
|
||||
return new Collection(items)
|
||||
}
|
||||
|
||||
keyBy(iteratee: Iteratee) {
|
||||
const items = _.keyBy(this._items, iteratee)
|
||||
|
||||
return new Dictionary(items)
|
||||
}
|
||||
|
||||
empty() {
|
||||
return this._items.length === 0
|
||||
}
|
||||
|
||||
includes(value: any) {
|
||||
if (typeof value === 'function') {
|
||||
const found = this._items.find(value)
|
||||
|
||||
return !!found
|
||||
}
|
||||
|
||||
return this._items.includes(value)
|
||||
}
|
||||
|
||||
missing(value: any) {
|
||||
if (typeof value === 'function') {
|
||||
const found = this._items.find(value)
|
||||
|
||||
return !found
|
||||
}
|
||||
|
||||
return !this._items.includes(value)
|
||||
}
|
||||
|
||||
uniqBy(iteratee: Iteratee) {
|
||||
const items = _.uniqBy(this._items, iteratee)
|
||||
|
||||
return new Collection(items)
|
||||
}
|
||||
|
||||
groupBy(iteratee: Iteratee) {
|
||||
const object = _.groupBy(this._items, iteratee)
|
||||
|
||||
return new Dictionary(object)
|
||||
}
|
||||
|
||||
map(iteratee: Iteratee) {
|
||||
const items = this._items.map(iteratee)
|
||||
|
||||
return new Collection(items)
|
||||
}
|
||||
|
||||
all() {
|
||||
return this._items
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return JSON.stringify(this._items)
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
export class Dictionary {
|
||||
dict: any
|
||||
|
||||
constructor(dict?: any) {
|
||||
this.dict = dict || {}
|
||||
}
|
||||
|
||||
set(key: string, value: any) {
|
||||
this.dict[key] = value
|
||||
}
|
||||
|
||||
has(key: string): boolean {
|
||||
return !!this.dict[key]
|
||||
}
|
||||
|
||||
missing(key: string): boolean {
|
||||
return !this.dict[key]
|
||||
}
|
||||
|
||||
get(key: string): any {
|
||||
return this.dict[key] ? this.dict[key] : undefined
|
||||
}
|
||||
|
||||
keys(): string[] {
|
||||
return Object.keys(this.dict)
|
||||
}
|
||||
|
||||
data() {
|
||||
return this.dict
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
import * as path from 'path'
|
||||
|
||||
export class File {
|
||||
filepath: string
|
||||
content: string
|
||||
|
||||
constructor(filepath: string, content?: string) {
|
||||
this.filepath = path.normalize(filepath)
|
||||
this.content = content || ''
|
||||
}
|
||||
|
||||
getFilename() {
|
||||
return path.parse(this.filepath).name
|
||||
}
|
||||
|
||||
dirname() {
|
||||
return path.dirname(this.filepath)
|
||||
}
|
||||
|
||||
basename() {
|
||||
return path.basename(this.filepath)
|
||||
}
|
||||
|
||||
append(data: string) {
|
||||
this.content = this.content + data
|
||||
}
|
||||
|
||||
extension() {
|
||||
return this.filepath.split('.').pop()
|
||||
}
|
||||
}
|
@ -1,13 +1,7 @@
|
||||
export * from './logger'
|
||||
export * from './playlistParser'
|
||||
export * from './numberParser'
|
||||
export * from './logParser'
|
||||
export * from './markdown'
|
||||
export * from './file'
|
||||
export * from './collection'
|
||||
export * from './dictionary'
|
||||
export * from './storage'
|
||||
export * from './url'
|
||||
export * from './issueLoader'
|
||||
export * from './issueParser'
|
||||
export * from './htmlTable'
|
||||
|
@ -1,9 +0,0 @@
|
||||
import signale from 'signale'
|
||||
|
||||
const { Signale } = signale
|
||||
|
||||
export class Logger extends Signale {
|
||||
constructor(options?: any) {
|
||||
super(options)
|
||||
}
|
||||
}
|
@ -1,84 +0,0 @@
|
||||
import { File, Collection } from './'
|
||||
import * as path from 'path'
|
||||
import fs from 'fs-extra'
|
||||
import { glob } from 'glob'
|
||||
|
||||
export class Storage {
|
||||
rootDir: string
|
||||
|
||||
constructor(rootDir?: string) {
|
||||
this.rootDir = path.normalize(rootDir || './')
|
||||
}
|
||||
|
||||
async list(pattern: string): Promise<string[]> {
|
||||
const files = await glob(pattern, {
|
||||
cwd: this.rootDir
|
||||
})
|
||||
|
||||
return files.sort()
|
||||
}
|
||||
|
||||
async createDir(dir: string): Promise<void> {
|
||||
if (await fs.exists(dir)) return
|
||||
|
||||
await fs.mkdir(dir, { recursive: true }).catch(console.error)
|
||||
}
|
||||
|
||||
async load(filepath: string): Promise<any> {
|
||||
return this.read(filepath)
|
||||
}
|
||||
|
||||
async read(filepath: string): Promise<any> {
|
||||
const absFilepath = path.join(this.rootDir, filepath)
|
||||
|
||||
return await fs.readFile(absFilepath, { encoding: 'utf8' })
|
||||
}
|
||||
|
||||
async json(filepath: string): Promise<any> {
|
||||
const absFilepath = path.join(this.rootDir, filepath)
|
||||
const content = await fs.readFile(absFilepath, { encoding: 'utf8' })
|
||||
|
||||
return JSON.parse(content)
|
||||
}
|
||||
|
||||
async exists(filepath: string): Promise<boolean> {
|
||||
const absFilepath = path.join(this.rootDir, filepath)
|
||||
|
||||
return await fs.exists(absFilepath)
|
||||
}
|
||||
|
||||
async write(filepath: string, data: string = ''): Promise<void> {
|
||||
const absFilepath = path.join(this.rootDir, filepath)
|
||||
const dir = path.dirname(absFilepath)
|
||||
|
||||
await this.createDir(dir)
|
||||
await fs.writeFile(absFilepath, data, { encoding: 'utf8', flag: 'w' })
|
||||
}
|
||||
|
||||
async append(filepath: string, data: string = ''): Promise<void> {
|
||||
const absFilepath = path.join(this.rootDir, filepath)
|
||||
|
||||
await fs.appendFile(absFilepath, data, { encoding: 'utf8', flag: 'w' })
|
||||
}
|
||||
|
||||
async clear(filepath: string): Promise<void> {
|
||||
await this.write(filepath)
|
||||
}
|
||||
|
||||
async createStream(filepath: string): Promise<NodeJS.WriteStream> {
|
||||
const absFilepath = path.join(this.rootDir, filepath)
|
||||
const dir = path.dirname(absFilepath)
|
||||
|
||||
await this.createDir(dir)
|
||||
|
||||
return fs.createWriteStream(absFilepath) as unknown as NodeJS.WriteStream
|
||||
}
|
||||
|
||||
async save(filepath: string, content: string): Promise<void> {
|
||||
await this.write(filepath, content)
|
||||
}
|
||||
|
||||
async saveFile(file: File): Promise<void> {
|
||||
await this.write(file.filepath, file.content)
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
import normalizeUrl from 'normalize-url'
|
||||
|
||||
export class URL {
|
||||
url: string
|
||||
|
||||
constructor(url: string) {
|
||||
this.url = url
|
||||
}
|
||||
|
||||
normalize(): URL {
|
||||
const normalized = normalizeUrl(this.url, { stripWWW: false })
|
||||
this.url = decodeURIComponent(normalized).replace(/\s/g, '+')
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.url
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue