import frameFile from "../data/frameworks.json" ; import libFile from "../data/libraries.json" ; import toolFile from "../data/tools.json" ; import cloudFile from "../data/cloud.json" ; import * as jmespath from "jmespath" ; const items = libFile . concat ( frameFile , toolFile , cloudFile ) ; interface SearchInterface { keyword : string ; type : "tools" | "libraries" | "frameworks" | "*" ; page ? : number ; } export class dataProvider { param limit returns getAllWithLimit ( limit ) { function shuffle ( array ) { array . sort ( ( ) => Math . random ( ) - 0.5 ) ; } shuffle ( items ) ; const data = items . slice ( 0 , limit ) ; return data ; } returns getAll ( ) { function shuffle ( array ) { array . sort ( ( ) => Math . random ( ) - 0.5 ) ; } shuffle ( items ) ; return items ; } param tags param page param type returns getBy ( tags , page , type ) { const startArray = 0 + page * 10 ; const endArray = page * 10 + 10 ; const dataTags = jmespath . search ( items , "[?tags.contains(@, '" + tags + "') && type.contains(@, '" + type + "') == `true`]" ) ; const dataFinal = jmespath . search ( dataTags , "[" + startArray + ":" + endArray + "]" ) ; return { result : dataFinal , nbr : dataTags . length } ; } param tags param page returns getByTags ( tags , page ) { const startArray = 0 + page * 10 ; const endArray = page * 10 + 10 ; const dataTags = jmespath . search ( items , "[?tags.contains(@, '" + tags + "') == `true`]" ) ; const dataFinal = jmespath . search ( dataTags , "[" + startArray + ":" + endArray + "]" ) ; return { result : dataFinal , nbr : dataTags . length } ; } param type param page returns getByType ( type , page ) { const startArray = 0 + page * 10 ; const endArray = page * 10 + 10 ; const dataTags = jmespath . search ( items , "[?type.contains(@, '" + type + "') == `true`]" ) ; const dataFinal = jmespath . search ( dataTags , "[" + startArray + ":" + endArray + "]" ) ; return { result : dataFinal , nbr : dataTags . length } ; } param s returns capitalize ( s ) { if ( typeof s !== 'string' ) return '' ; return s . charAt ( 0 ) . toUpperCase ( ) + s . slice ( 1 ) ; } param SearchObject search ( SearchObject ) { const startArray = 0 + SearchObject . page * 10 ; const endArray = SearchObject . page * 10 + 10 ; SearchObject . keyword = this . capitalize ( SearchObject . keyword ) ; if ( SearchObject . type == "*" ) { let search = jmespath . search ( items , "[?mini_desc.contains(@, '" + SearchObject . keyword + "') || tags.contains(@, '" + SearchObject . keyword + "') || name.contains(@, '" + SearchObject . keyword + "') == `true`]" ) ; const dataFinal = jmespath . search ( search , "[" + startArray + ":" + endArray + "]" ) ; return { result : dataFinal , nbr : search . length } ; } else { let byKeyword = jmespath . search ( items , "[?mini_desc.contains(@, '" + SearchObject . keyword + "') || tags.contains(@, '" + SearchObject . keyword + "') || name.contains(@, '" + SearchObject . keyword + "') == `true`]" ) ; let byType = jmespath . search ( byKeyword , "[?type.contains(@, '" + SearchObject . type + "') == `true`]" ) ; const dataFinal = jmespath . search ( byType , "[" + startArray + ":" + endArray + "]" ) ; return { result : dataFinal , nbr : byType . length } ; } } }