@vbarbarosh/node-helpers 3.13.0 → 3.14.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.
package/package.json
CHANGED
|
@@ -3,17 +3,28 @@ const stream = require('stream');
|
|
|
3
3
|
const wait_while = require('./wait_while');
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Returns a writable stream for
|
|
6
|
+
* Returns a writable stream for inserting, or conditionally updating, documents in mongo collection.
|
|
7
7
|
*/
|
|
8
|
-
function mongo_stream_upsert({collection, concurrency = 1})
|
|
8
|
+
function mongo_stream_upsert({collection, update_if, concurrency = 1})
|
|
9
9
|
{
|
|
10
|
+
const errors = [];
|
|
10
11
|
return new stream.Writable({
|
|
11
12
|
objectMode: true,
|
|
12
13
|
construct(next) {
|
|
13
14
|
this.running = 0;
|
|
14
15
|
next();
|
|
15
16
|
},
|
|
17
|
+
destroy: async function (error, next) {
|
|
18
|
+
// console.log('mongo_stream_upsert.destroy', errors);
|
|
19
|
+
await wait_while(() => this.running > 0);
|
|
20
|
+
next();
|
|
21
|
+
},
|
|
16
22
|
write: async function (items, enc, next) {
|
|
23
|
+
// console.log('mongo_stream_upsert.write', errors);
|
|
24
|
+
if (errors.length) {
|
|
25
|
+
next(errors[0]);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
17
28
|
try {
|
|
18
29
|
if (!Array.isArray(items)) {
|
|
19
30
|
next(new Error('An array of objects is expected.'));
|
|
@@ -21,10 +32,33 @@ function mongo_stream_upsert({collection, concurrency = 1})
|
|
|
21
32
|
}
|
|
22
33
|
await wait_while(() => this.running >= concurrency);
|
|
23
34
|
const operations = items.map(function (item) {
|
|
24
|
-
|
|
35
|
+
if (!update_if) {
|
|
36
|
+
return {
|
|
37
|
+
replaceOne: {
|
|
38
|
+
filter: {_id: item._id},
|
|
39
|
+
replacement: item,
|
|
40
|
+
upsert: true,
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
updateOne: {
|
|
46
|
+
filter: {_id: item._id},
|
|
47
|
+
update: [[
|
|
48
|
+
{
|
|
49
|
+
$replaceRoot: {
|
|
50
|
+
newRoot: {
|
|
51
|
+
$cond: [update_if, item, '$$ROOT'],
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
]],
|
|
56
|
+
upsert: true,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
25
59
|
});
|
|
26
60
|
this.running++;
|
|
27
|
-
Promise.resolve(collection.bulkWrite(operations, {multi: true})).finally(() => this.running--);
|
|
61
|
+
Promise.resolve(collection.bulkWrite(operations, {multi: true})).catch(e => errors.push(e)).finally(() => this.running--);
|
|
28
62
|
next();
|
|
29
63
|
}
|
|
30
64
|
catch (error) {
|
|
@@ -32,6 +66,11 @@ function mongo_stream_upsert({collection, concurrency = 1})
|
|
|
32
66
|
}
|
|
33
67
|
},
|
|
34
68
|
final: async function (next) {
|
|
69
|
+
// console.log('mongo_stream_upsert.final', errors);
|
|
70
|
+
if (errors.length) {
|
|
71
|
+
next(errors[0]);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
35
74
|
await wait_while(() => this.running > 0);
|
|
36
75
|
next();
|
|
37
76
|
},
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
function sanitize_dash_name(s)
|
|
2
|
+
{
|
|
3
|
+
// https://stackoverflow.com/a/37511463
|
|
4
|
+
return s.normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/[^a-zA-Z0-9]/g, '-').replace(/-+/g, '-').replace(/^-+|-+$/g, '').toLowerCase();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
module.exports = sanitize_dash_name;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const sanitize_dash_name = require('./sanitize_dash_name');
|
|
3
|
+
|
|
4
|
+
const items = [
|
|
5
|
+
['', ''],
|
|
6
|
+
['', '.'],
|
|
7
|
+
['', 'привет'],
|
|
8
|
+
['1-2-3', '1-2-3'],
|
|
9
|
+
['1-2-3', '1.2.3'],
|
|
10
|
+
['1000-stories', '1000 Stories'],
|
|
11
|
+
['1800', '1800'],
|
|
12
|
+
['3-a', '3-A'],
|
|
13
|
+
['43', '43'],
|
|
14
|
+
['600-d', '600+D'],
|
|
15
|
+
['9-lives', '9 Lives'],
|
|
16
|
+
['a-c-d', 'A, C, D'],
|
|
17
|
+
['a-f', 'A F'],
|
|
18
|
+
['accua-aseptic-solucion', 'Accua Aséptic Solución'],
|
|
19
|
+
['acido-acetilsalicilico', 'Ácido Acetilsalicílico'],
|
|
20
|
+
['acido-nalidixico-fenazopiridina', 'Ácido Nalidixico-Fenazopiridina'],
|
|
21
|
+
['aeropostale', 'Aéropostale'],
|
|
22
|
+
['arnica-nartex', 'Árnica Nártex'],
|
|
23
|
+
['art-n-chef', 'Art \'n Chef'],
|
|
24
|
+
['b-a-r-t-e-r', 'B.A.R.T.E.R.'],
|
|
25
|
+
['b-oost', 'b:oost'],
|
|
26
|
+
['betametasona-clotrimazol-gentamicina', 'Betametasona - Clotrimazol - Gentamicina'],
|
|
27
|
+
['black-decker', 'Black & Decker'],
|
|
28
|
+
['black-decker', 'Black + Decker'],
|
|
29
|
+
['black-panther', 'Black Panther'],
|
|
30
|
+
['black-white', 'Black & White'],
|
|
31
|
+
['blanca-nieves', 'Blanca Nieves'],
|
|
32
|
+
['de-rancho', 'de Rancho'],
|
|
33
|
+
['del-valle', 'del Valle'],
|
|
34
|
+
['exportacion-selecto', 'Exportación Selecto'],
|
|
35
|
+
['extra', 'Extra'],
|
|
36
|
+
['ezetimiba-simvastatina', 'Ezetimiba Simvastatina'],
|
|
37
|
+
['facicam-gel-90g', 'FACICAM GEL 90G'],
|
|
38
|
+
['folios', 'FOLIOS'],
|
|
39
|
+
['fresh-garden', 'FRESH GARDEN'],
|
|
40
|
+
['gi-valproato-de-magnesio', '(GI) Valproato de Magnesio'],
|
|
41
|
+
['itrac-100mg-c-f-15c', 'ITRAC 100MG C/F 15C'],
|
|
42
|
+
['m-m-s', 'M&M\'s'],
|
|
43
|
+
['m-m-s', 'M&M´s'],
|
|
44
|
+
['meticel-0-5', 'Meticel 0.5 %'],
|
|
45
|
+
['meticel-2', 'Meticel 2%'],
|
|
46
|
+
['opera-prima', 'Ópera Prima'],
|
|
47
|
+
['oxido-de-zinc', 'Óxido de Zinc'],
|
|
48
|
+
['r-u-m-b-a', 'r u m b a'],
|
|
49
|
+
['r-u-m-b-a', 'r_u_m_b_a'],
|
|
50
|
+
['ssh-authorized-keys', '~/.\u0000ssh/authorized_keys";\n'],
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
describe('sanitize_dash_name', function () {
|
|
54
|
+
items.forEach(function ([expected, input],) {
|
|
55
|
+
it(`${input} → ${expected}`, function () {
|
|
56
|
+
assert.deepStrictEqual(sanitize_dash_name(input), expected);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|