UNPKG

1.86 kBJavaScriptView Raw
1import { samplesFind } from '@analys/samples-find';
2import { samplesFormula } from '@analys/samples-formula';
3import { samplesGroup } from '@analys/samples-group';
4import { samplesPivot } from '@analys/samples-pivot';
5import { samplesSelect } from '@analys/samples-select';
6
7class Samples {
8 constructor(samples, title, types) {
9 this.title = title;
10 this.data = samples;
11 if (types) this.types = types;
12 }
13
14 get length() {
15 return this.data.length;
16 }
17
18 static from(samples) {
19 return new Samples(samples);
20 }
21
22 select(fields, {
23 mutate = true
24 } = {}) {
25 const data = samplesSelect(this.data, fields);
26 return mutate ? this.boot({
27 data,
28 types: []
29 }) : this.copy({
30 data,
31 types: []
32 });
33 }
34
35 find(filter, {
36 mutate = true
37 } = {}) {
38 const data = samplesFind.call(this.data, filter);
39 return mutate ? this.boot({
40 data
41 }) : this.copy({
42 data
43 });
44 }
45
46 formula(formulae, configs = {}) {
47 return Samples.from(samplesFormula.call(this.data, formulae, configs));
48 }
49
50 group(configs) {
51 return Samples.from(samplesGroup.call(this.data, configs));
52 }
53
54 crosTab(tablespec) {
55 return samplesPivot.call(this.data, tablespec);
56 }
57 /** @returns {Samples} */
58
59
60 boot({
61 data,
62 types
63 } = {}, mutate) {
64 if (mutate) {
65 if (data) this.data = data;
66 if (types) this.types = types;
67 return this;
68 } else {
69 return this.copy({
70 data,
71 types
72 });
73 }
74 }
75 /** @returns {Samples} */
76
77
78 copy({
79 data,
80 types
81 } = {}) {
82 var _this$types;
83
84 if (!data) data = this.data.map(sample => Object.assign({}, sample));
85 if (!types) types = (_this$types = this.types) === null || _this$types === void 0 ? void 0 : _this$types.slice();
86 return new Samples(data, this.title, types);
87 }
88
89}
90
91export { Samples };