svelte-map-reduce-store
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -1,3 +0,3 @@ | ||
import type { SortCmd } from './fast-sort.js'; | ||
import * as dic from './dic.js'; | ||
import type { SortCmd } from './fast-sort'; | ||
import * as dic from './dic'; | ||
export declare type BaseT<IdType> = { | ||
@@ -11,2 +11,3 @@ _id: IdType; | ||
format: () => F; | ||
initialize?: (doc: F['list'][number]) => void; | ||
reduce: (o: F, doc: F['list'][number]) => void; | ||
@@ -20,2 +21,3 @@ order: (o: F, utils: typeof OrderUtils, ...args: OrderArgs) => void; | ||
}; | ||
declare type Validator<A extends any[], F extends BaseF<any>> = (o: F['list'][number], ...args: A) => boolean; | ||
declare type LookupProps<F, OrderArgs extends any[]> = { | ||
@@ -32,8 +34,17 @@ format: () => F; | ||
}; | ||
export declare function MapReduce<F extends BaseF<any>, OrderArgs extends any[]>({ format, reduce, order, start }: MapReduceProps<F, OrderArgs>): { | ||
deploy: (json: any) => void; | ||
add: (docs: F['list'][number][]) => void; | ||
export declare function MapReduce<F extends BaseF<any>, OrderArgs extends any[]>({ format, initialize, reduce, order, start }: MapReduceProps<F, OrderArgs>): { | ||
deploy: (json: any, init?: (doc: F["list"][number]) => void) => void; | ||
clear: () => void; | ||
add: (docs: F['list'], init?: (doc: F["list"][number]) => void) => void; | ||
del: (ids: F['list'][number]['_id'][]) => void; | ||
find: (id: F['list'][number]['_id']) => F["list"][number]; | ||
reduce: <EMIT>(ids: F['list'][number]['_id'][], emit: (o: EMIT) => void) => SortCmd<F["list"][number] & EMIT>; | ||
filter: <A extends any[]>(validator: Validator<A, F>, key?: string) => (...filter_args: A) => { | ||
reduce: <EMIT>(ids: F['list'][number]['_id'][], emit: (o: EMIT) => void) => SortCmd<F["list"][number] & EMIT>; | ||
filter: any; | ||
sort: (...sa: OrderArgs) => void; | ||
data: F; | ||
subscribe: (this: void, run: import("svelte/store").Subscriber<F>, invalidate?: (value?: F) => void) => import("svelte/store").Unsubscriber; | ||
validator: Validator<A, F>; | ||
}; | ||
sort: (...sa: OrderArgs) => void; | ||
@@ -40,0 +51,0 @@ format: () => F; |
55
base.js
import { writable } from 'svelte/store'; | ||
import { __BROWSER__ } from 'svelte-giji-browser-device'; | ||
import * as dic from './dic.js'; | ||
import { __BROWSER__ } from 'svelte-petit-utils'; | ||
import * as dic from './dic'; | ||
export const OrderUtils = { | ||
@@ -8,2 +8,3 @@ sort: dic.sort, | ||
}; | ||
function nop(...args) { } | ||
export function lookup(o) { | ||
@@ -25,3 +26,4 @@ const data = o.format(); | ||
} | ||
export function MapReduce({ format, reduce, order, start }) { | ||
export function MapReduce({ format, initialize = nop, reduce, order, start }) { | ||
const children = new Map(); | ||
const map = new Map(); | ||
@@ -32,3 +34,3 @@ const data = format(); | ||
let sArgs = []; | ||
return { deploy, add, del, find, reduce: doReduce, sort, format, data, subscribe }; | ||
return { deploy, clear, add, del, find, reduce: doReduce, filter, sort, format, data, subscribe }; | ||
function sort(...sa) { | ||
@@ -41,3 +43,3 @@ if (order) | ||
const { list } = data; | ||
Object.assign(data, format()); | ||
clear(); | ||
for (const doc of list) { | ||
@@ -48,3 +50,3 @@ data.list.push(doc); | ||
} | ||
function deploy(json) { | ||
function deploy(json, init = initialize) { | ||
const list = []; | ||
@@ -56,4 +58,21 @@ for (const _id in json) { | ||
} | ||
add(list); | ||
add(list, init); | ||
} | ||
function filter(validator, key = validator.toString()) { | ||
return query; | ||
function query(...filter_args) { | ||
const child = MapReduce({ format, reduce, order }); | ||
children.set(key, { validator, filter_args, add: child.add, del: child.del }); | ||
// child.clear() | ||
child.add(data.list.filter((o) => validator(o, ...filter_args))); | ||
return { | ||
reduce: child.reduce, | ||
filter: child.filter, | ||
sort: child.sort, | ||
data: child.data, | ||
subscribe: child.subscribe, | ||
validator | ||
}; | ||
} | ||
} | ||
function doReduce(ids, emit) { | ||
@@ -67,3 +86,3 @@ const map = new Map(); | ||
} | ||
const list = Array.from(map.values()); | ||
const list = [...map.values()]; | ||
for (const item of list) { | ||
@@ -74,3 +93,7 @@ emit(item); | ||
} | ||
function add(docs) { | ||
function clear() { | ||
Object.assign(data, format()); | ||
set(data); | ||
} | ||
function add(docs, init = initialize) { | ||
let is_update = false; | ||
@@ -84,2 +107,3 @@ for (const doc of docs) { | ||
data.list.push(doc); | ||
init && init(doc); | ||
reduce(data, doc); | ||
@@ -93,10 +117,19 @@ } | ||
set(data); | ||
for (const { validator, filter_args, add } of children.values()) { | ||
add(docs.filter((o) => validator(o, ...filter_args))); | ||
} | ||
} | ||
function del(ids) { | ||
let is_update = false; | ||
for (const id of ids) { | ||
map.delete(id); | ||
if (map.delete(id)) | ||
is_update = true; | ||
} | ||
full_calculate(); | ||
if (is_update) | ||
full_calculate(); | ||
set(data); | ||
for (const { del } of children.values()) { | ||
del(ids); | ||
} | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
import type { SortCmd } from './fast-sort.js'; | ||
import type { SortCmd } from './fast-sort'; | ||
export declare type DIC<T> = { | ||
@@ -3,0 +3,0 @@ [id: string]: T; |
@@ -1,2 +0,2 @@ | ||
import { inPlaceSort } from './fast-sort.js'; | ||
import { inPlaceSort } from './fast-sort'; | ||
export function sort(value) { | ||
@@ -3,0 +3,0 @@ if (!(value instanceof Array)) { |
@@ -1,5 +0,5 @@ | ||
export type { BaseT, BaseF } from './base.js'; | ||
export type { DIC } from './dic.js'; | ||
export * from './base.js'; | ||
export * from './dic.js'; | ||
export * from './group-by.js'; | ||
export type { BaseT, BaseF } from './base'; | ||
export type { DIC } from './dic'; | ||
export * from './base'; | ||
export * from './dic'; | ||
export * from './group-by'; |
@@ -1,3 +0,3 @@ | ||
export * from './base.js'; | ||
export * from './dic.js'; | ||
export * from './group-by.js'; | ||
export * from './base'; | ||
export * from './dic'; | ||
export * from './group-by'; |
{ | ||
"name": "svelte-map-reduce-store", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"homepage": "https://github.com/7korobi/svelte-giji/tree/master/src/lib/map-reduce", | ||
"license": "MIT", | ||
"type": "module", | ||
"keywords": [ | ||
"svelte", | ||
"component", | ||
"map-reduce" | ||
], | ||
"devDependencies": { | ||
"typescript": "^4.5.4" | ||
"svelte-preprocess": "^4.10.3", | ||
"typescript": "^4.5.5" | ||
}, | ||
"dependencies": { | ||
"svelte": "^3.44.3", | ||
"svelte-giji-browser-device": "^0.2.3", | ||
"svelte": "^3.46.4", | ||
"svelte-petit-utils": "^0.1.0", | ||
"tslib": "^2.3.1" | ||
}, | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": "./index.js", | ||
"./base": "./base.js", | ||
"./dic": "./dic.js", | ||
"./fast-sort": "./fast-sort.js", | ||
"./group-by": "./group-by.js", | ||
".": "./index.js", | ||
"./package.json": "./package.json" | ||
} | ||
"./group-by": "./group-by.js" | ||
}, | ||
"svelte": "./index.js" | ||
} |
@@ -34,4 +34,4 @@ ## Svelte Map Reduce Store | ||
order: (data, { sort, group_sort }, mode: 'asc' | 'desc') => { | ||
data.list = sort(data.list)[mode]((o)=> o.server) | ||
} | ||
data.list = sort(data.list)[mode]((o) => o.server) | ||
} | ||
}) | ||
@@ -42,31 +42,35 @@ ``` | ||
<script lang="ts"> | ||
import { Folders } from './model' | ||
import { Folders } from './model' | ||
Folders.add([{ | ||
_id: 'hello', | ||
server: 'https://localhost/hello-world' | ||
}, { | ||
_id: 'test', | ||
server: 'https://localhost/testpage' | ||
}]) | ||
Folders.add([ | ||
{ | ||
_id: 'hello', | ||
server: 'https://localhost/hello-world' | ||
}, | ||
{ | ||
_id: 'test', | ||
server: 'https://localhost/testpage' | ||
} | ||
]) | ||
Folders.deploy({ | ||
hello: { | ||
server: 'https://localhost/hello-world' | ||
}, | ||
test: { | ||
server: 'https://localhost/testpage' | ||
} | ||
}) | ||
Folders.deploy({ | ||
hello: { | ||
server: 'https://localhost/hello-world' | ||
}, | ||
test: { | ||
server: 'https://localhost/testpage' | ||
} | ||
}) | ||
Folders.find('hello') | ||
Folders.reduce(['hello', 'test'], (o: { count: number })=>{ o.count ||= 0; o.count++ }).asc((o)=> o.count) | ||
Folders.find('hello') | ||
Folders.reduce(['hello', 'test'], (o: { count: number }) => { | ||
o.count ||= 0 | ||
o.count++ | ||
}).asc((o) => o.count) | ||
Folders.sort('desc') | ||
Folders.sort('asc') | ||
Folders.sort('desc') | ||
Folders.sort('asc') | ||
$: console.log($Folders.list) | ||
$: console.log($Folders.list) | ||
</script> | ||
``` | ||
``` |
29704
536
75
2
12
+ Addedsvelte-petit-utils@^0.1.0
+ Addedsvelte-petit-utils@0.1.8(transitive)
- Removedsvelte-giji-browser-device@^0.2.3
Updatedsvelte@^3.46.4