og-statistics 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +27 -0
  3. package/package.json +18 -0
  4. package/src/index.js +183 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # og-statistics
2
+
3
+ > Descripción del paquete
4
+
5
+ ## Instalación
6
+
7
+ ```bash
8
+ npm install og-statistics
9
+ ```
10
+
11
+ ## Uso
12
+
13
+ ```js
14
+ const { hello } = require('og-statistics');
15
+
16
+ hello('world'); // "Hello, world!"
17
+ ```
18
+
19
+ ## API
20
+
21
+ ### `hello(name)`
22
+
23
+ Descripción de la función.
24
+
25
+ ## License
26
+
27
+ MIT
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "og-statistics",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "./src/index.js",
6
+ "scripts": {
7
+ "test": "node test/index.test.js"
8
+ },
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "MIT",
12
+ "files": [
13
+ "src/"
14
+ ],
15
+ "engines": {
16
+ "node": ">=18.0.0"
17
+ }
18
+ }
package/src/index.js ADDED
@@ -0,0 +1,183 @@
1
+ 'use strict';
2
+
3
+ /*
4
+ filterStats se utiliza para clasificar elementos descritos en 'keys'
5
+ responde la siguiente estructua json
6
+ fouls: { local: 12, away: 19 },
7
+ cornerKicks: { local: 5, away: 8 },
8
+ totalShotsOnGoal: { local: 9, away: 13 },
9
+ shotsOnGoal: { local: 2, away: 3 },
10
+ hitWoodwork: { local: 0, away: 1 },
11
+ shotsOffGoal: { local: 4, away: 6 },
12
+ blockedScoringAttempt: { local: 3, away: 4 },
13
+ errorLeadToGoal: { local: 0, away: 0 }
14
+ */
15
+
16
+ /*
17
+ Match overview:
18
+ - ballPossession
19
+ - expectedGoals
20
+ - cornerKicks
21
+ - passes
22
+
23
+ Shots:
24
+ - totalShotsOnGoal (total de tiros)
25
+ - shotsOnGoal (tiros a portería)
26
+ - hitWoodwork (tiros a poste)
27
+ - shotsOffGoal (tiros fuera)
28
+ - totalShotsInsideBox (tiros en el area)
29
+ - totalShotsOutsideBox (tiros fuera del area)
30
+
31
+ Attack:
32
+ - bigChanceScored (oportunidades anotadas)
33
+ - bigChanceMissed (oportunidades perdidas)
34
+ - offsides (fuera de lugar)
35
+
36
+ Passes:
37
+ - accuratePasses (Pases acertados)
38
+ - throwIns (Saque de banda)
39
+ - finalThirdPhaseStatistic (entradas ultimo tercio)
40
+
41
+
42
+ */
43
+
44
+ let def = [
45
+ "ballPossession",
46
+ "expectedGoals",
47
+ "cornerKicks",
48
+ "passes",
49
+ "totalShotsOnGoal",
50
+ "shotsOnGoal",
51
+ "hitWoodwork",
52
+ "shotsOffGoal",
53
+ "totalShotsInsideBox",
54
+ "totalShotsOutsideBox",
55
+ "bigChanceScored",
56
+ "bigChanceMissed",
57
+ "offsides",
58
+ "accuratePasses",
59
+ "throwIns",
60
+ "finalThirdEntries",
61
+ "errorsLeadToShot",
62
+ "errorsLeadToGoal"
63
+ ];
64
+
65
+ function filterStats(statsBase, keys = def, fallback = 0) {
66
+ try {
67
+ // Definición interna de los periodos
68
+ if (!statsBase || !keys || statsBase.toString().trim() === "" || keys.toString().trim() === "") {
69
+ return { ok: false, error: { code: "missing_context", message: "Error en los periodos" } };
70
+ }
71
+ const periods = { all: 0, ft: 1, st: 2 };
72
+ const result = {};
73
+
74
+ for (const [label, idx] of Object.entries(periods)) {
75
+ const groups = statsBase?.statistics?.[idx]?.groups ?? [];
76
+ const items = groups.flatMap(g => g.statisticsItems);
77
+
78
+ // índice: se queda con el primero encontrado por key
79
+ const firstByKey = {};
80
+ for (const it of items) {
81
+ if (firstByKey[it.key] === undefined) firstByKey[it.key] = it;
82
+ }
83
+
84
+ const bucket = {};
85
+ for (const k of keys) {
86
+ const it = firstByKey[k];
87
+ bucket[k] = {
88
+ home: it?.homeValue ?? fallback,
89
+ away: it?.awayValue ?? fallback,
90
+ };
91
+ }
92
+
93
+ result[label] = bucket;
94
+ }
95
+
96
+ return result;
97
+ } catch (error) {
98
+ return { ok: false, error: { code: "filterStats", message: error.message, stack: error.stack } };
99
+ }
100
+ }
101
+ /*
102
+ card
103
+ */
104
+
105
+ function filterIncidents(incidents) {
106
+ try {
107
+ //Cards, goals, substitutions
108
+
109
+ const homeCards = filterCards(incidents.incidents, true);
110
+ const awayCards = filterCards(incidents.incidents, false);
111
+
112
+ const homeGoals = filterGoals(incidents.incidents, true);
113
+ const awayGoals = filterGoals(incidents.incidents, false);
114
+
115
+ const homeSubs = filterSubs(incidents.incidents, true);
116
+ const awaySubs = filterSubs(incidents.incidents, false);
117
+
118
+ return {
119
+ home: { cards: homeCards, goals: homeGoals, subs: homeSubs },
120
+ away: { cards: awayCards, goals: awayGoals, subs: awaySubs },
121
+
122
+
123
+ }
124
+
125
+ } catch (error) {
126
+ return { ok: false, error: { code: error.message, message: error.stack } }
127
+ }
128
+ }
129
+
130
+
131
+ const CARD_CLASSES = ['red', 'yellow', 'yellowRed'];
132
+
133
+ const filterCards = (incidents, isHome) => {
134
+ const teamIncidents = incidents.filter(s => s.isHome === isHome);
135
+
136
+ const cards = CARD_CLASSES.reduce((acc, cardClass) => {
137
+ const all = teamIncidents.filter(s => s.incidentType === 'card' && s.incidentClass === cardClass);
138
+ const ft = all.filter(s => s.time <= 45);
139
+ const st = all.filter(s => s.time > 45);
140
+
141
+ acc[cardClass] = {
142
+ all: all,
143
+ ft: ft,
144
+ st: st,
145
+ };
146
+
147
+ return acc;
148
+ }, {});
149
+
150
+ cards.fulltime = teamIncidents;
151
+
152
+ return cards;
153
+ };
154
+
155
+ const GOAL_CLASSES = ['regular', 'penalty', 'ownGoal'];
156
+
157
+ const filterGoals = (incidents, isHome) => {
158
+ const teamIncidents = incidents.filter(s => s.isHome === isHome && s.incidentType === 'goal');
159
+
160
+ const goles = GOAL_CLASSES.reduce((acc, cls) => {
161
+ const all = teamIncidents.filter(s => s.incidentClass === cls);
162
+
163
+ acc[cls] = all;
164
+
165
+ return acc
166
+ }, {});
167
+
168
+ goles.full = teamIncidents;
169
+ return goles;
170
+ };
171
+
172
+ function filterSubs(incidents, isHome) {
173
+ const teamIncidents = incidents.filter(s => s.isHome === isHome && s.incidentType === 'substitution');
174
+ const teamInjured = incidents.filter(s => s.isHome === isHome && s.incidentType === 'substitution' && s.injury === true);
175
+
176
+ return {
177
+ subs: teamIncidents,
178
+ injured: teamInjured
179
+ }
180
+ }
181
+
182
+
183
+ module.exports = { filterStats, filterIncidents };