Dann eben so ...
bearbeitet von Raketenwilli> Meinereiner ist zumindest zeitweise in Overcarefullington sozialisiert worden und möchte auf die richtige Schreibweise der `needles` hinweisen…
Das ist schnell nachgebessert:. Außerdem habe ich mir die Frechheit erlaubt, bei mir nicht gefallenden Daten ein paar Ausnahmen zu schmeißen.
~~~js
function CharCounter( haystack, needles=false, log=false ) {
var counter={};
if (
null === needles
|| false === needles
|| true === needles
|| '' == needles
) {
throw( 'Error: needles is exact null, true, false - or empty' ) ;
}
if ( 'string' == typeof needles ) {
needles = needles.split('');
}
if ( ! Array.isArray( needles ) ) {
needles[0] = needles;
} else {
console.log( 'Gefunden: ' + JSON.stringify( needles ) ) ;
}
for ( var i=0; i < needles.length; i++ ) {
if ( 'string' == typeof needles[i] ) {
if ( 1 < needles[i].length ) {
throw('Error: String with length > 1 in array needles[' + i + ']: ' + JSON.stringify( needles[i] ) ) ;
} else if ( 1 == needles[i].length ) {
counter[needles[i]] = 0;
} else {
console.warn( 'Warníng: Empty string-element in array needles at position ' + i + ' ignored.' );
}
} else {
throw( 'Error: Element at position ' + i + ' in array needles is not of type string: It is a ' + typeof( needles[i] ) + '. Value(s): '+ JSON.stringify( needles[i] ) );
}
}
needles = Object.keys(counter);
needles.sort();
haystack.split('').forEach(
function ( item, index, arr ) {
for ( var i = 0; i <= needles.length; i++ ) {
if( item == needles[i] ) {
counter[needles[i]]++;
}
}
}
);
if ( log ) {
for ( i=0; i < needles.length; i++ ) {
console.log(
'Char »'
+ needles[i]
+ '« has '
+ counter[needles[i]]
+ ' founds.'
);
}
}
return counter;
}
str = 'arg1.forEach(wert => (wert % 2 ? anzUngerade : anzGerade).push(korr005(wert));';
CharCounter( str, '()', true );
/////////////////// Tests: /////////////////////////////////////////////////////////////////
//CharCounter( str, '()[]{}\'"', true ); /* Fine */
//CharCounter( str, str , true ); /* Fine: Count all Chars in haystack */
//CharCounter( str, [' ',''] , true ); /* Fine with warning */
//CharCounter( str, null , true ); /* Error */
//CharCounter( str, true , true ); /* Error */
//CharCounter( str, ['(', null ] , true ); /* Error */
//CharCounter( str, [' ', 100.8 ], true ); /* Error */
//CharCounter( str, [ ' ', [ 'f','c' ] ], true ); /* Error */
~~~
Ausgabe:
~~~text,bad
Char »(« has 4 founds.
Char »)« has 3 founds.
~~~
Wichtiger wäre die Frage, [warum ich die Klammern zähle](https://forum.selfhtml.org/self/2022/jul/28/ruckspeichern-in-argument/1800785#m1800785)...
Das ist schnell nachgebessert
~~~js
function CharCounter( haystack, needles=false, log=false ) {
var counter={};
if (
null === needles
|| false === needles
|| true === needles
|| '' == needles
) {
throw( 'Error: needles is exact null, true, false - or empty' ) ;
}
if ( 'string' == typeof needles ) {
needles = needles.split('');
}
if ( ! Array.isArray( needles ) ) {
needles[0] = needles;
} else {
console.log( 'Gefunden: ' + JSON.stringify( needles ) ) ;
}
for ( var i=0; i < needles.length; i++ ) {
if ( 'string' == typeof needles[i] ) {
if ( 1 < needles[i].length ) {
throw('Error: String with length > 1 in array needles[' + i + ']: ' + JSON.stringify( needles[i] ) ) ;
} else if ( 1 == needles[i].length ) {
counter[needles[i]] = 0;
} else {
console.warn( 'Warníng: Empty string-element in array needles at position ' + i + ' ignored.' );
}
} else {
throw( 'Error: Element at position ' + i + ' in array needles is not of type string: It is a ' + typeof( needles[i] ) + '. Value(s): '+ JSON.stringify( needles[i] ) );
}
}
needles = Object.keys(counter);
needles.sort();
haystack.split('').forEach(
function ( item, index, arr ) {
for ( var i = 0; i <= needles.length; i++ ) {
if( item == needles[i] ) {
counter[needles[i]]++;
}
}
}
);
if ( log ) {
for ( i=0; i < needles.length; i++ ) {
console.log(
'Char »'
+ needles[i]
+ '« has '
+ counter[needles[i]]
+ ' founds.'
);
}
}
return counter;
}
str = 'arg1.forEach(wert => (wert % 2 ? anzUngerade : anzGerade).push(korr005(wert));';
CharCounter( str, '()', true );
/////////////////// Tests: /////////////////////////////////////////////////////////////////
//CharCounter( str, '()[]{}\'"', true ); /* Fine */
//CharCounter( str, str , true ); /* Fine: Count all Chars in haystack */
//CharCounter( str, [' ',''] , true ); /* Fine with warning */
//CharCounter( str, null , true ); /* Error */
//CharCounter( str, true , true ); /* Error */
//CharCounter( str, ['(', null ] , true ); /* Error */
//CharCounter( str, [' ', 100.8 ], true ); /* Error */
//CharCounter( str, [ ' ', [ 'f','c' ] ], true ); /* Error */
~~~
Ausgabe:
~~~text,bad
Char »(« has 4 founds.
Char »)« has 3 founds.
~~~
Wichtiger wäre die Frage, [warum ich die Klammern zähle](https://forum.selfhtml.org/self/2022/jul/28/ruckspeichern-in-argument/1800785#m1800785)...
Dann eben so ...
bearbeitet von Raketenwilli> Meinereiner ist zumindest zeitweise in Overcarefullington sozialisiert worden und möchte auf die richtige Schreibweise der `needles` hinweisen…
Das ist schnell nachgebessert:
~~~js
function CharCounter( haystack, needles=false, log=false ) {
var counter={};
if (
null === needles
|| false === needles
|| true === needles
|| '' == needles
) {
throw( 'Error: needles is exact null, true, false - or empty' ) ;
}
if ( 'string' == typeof needles ) {
needles = needles.split('');
}
if ( ! Array.isArray( needles ) ) {
needles[0] = needles;
} else {
console.log( 'Gefunden: ' + JSON.stringify( needles ) ) ;
}
for ( var i=0; i < needles.length; i++ ) {
if ( 'string' == typeof needles[i] ) {
if ( 1 < needles[i].length ) {
throw('Error: String with length > 1 in array needles[' + i + ']: ' + JSON.stringify( needles[i] ) ) ;
} else if ( 1 == needles[i].length ) {
counter[needles[i]] = 0;
} else {
console.warn( 'Warníng: Empty string-element in array needles at position ' + i + ' ignored.' );
}
} else {
throw( 'Error: Element at position ' + i + ' in array needles is not of type string: It is a ' + typeof( needles[i] ) + '. Value(s): '+ JSON.stringify( needles[i] ) );
}
}
needles = Object.keys(counter);
needles.sort();
haystack.split('').forEach(
function ( item, index, arr ) {
for ( var i = 0; i <= needles.length; i++ ) {
if( item == needles[i] ) {
counter[needles[i]]++;
}
}
}
);
if ( log ) {
for ( i=0; i < needles.length; i++ ) {
console.log(
'Char »'
+ needles[i]
+ '« has '
+ counter[needles[i]]
+ ' founds.'
);
}
}
return counter;
}
str = 'arg1.forEach(wert => (wert % 2 ? anzUngerade : anzGerade).push(korr005(wert));';
CharCounter( str, '()', true );
/////////////////// Tests: /////////////////////////////////////////////////////////////////
//CharCounter( str, '()[]{}\'"', true ); /* Fine */
//CharCounter( str, str , true ); /* Fine: Count all Chars in haystack */
//CharCounter( str, [' ',''] , true ); /* Fine with warning */
//CharCounter( str, null , true ); /* Error */
//CharCounter( str, true , true ); /* Error */
//CharCounter( str, ['(', null ] , true ); /* Error */
//CharCounter( str, [' ', 100.8 ], true ); /* Error */
//CharCounter( str, [ ' ', [ 'f','c' ] ], true ); /* Error */
~~~
Ausgabe:
~~~text,bad
Char »(« has 4 founds.
Char »)« has 3 founds.
~~~
Wichtiger wäre die Frage, [warum ich die Klammern zähle](https://forum.selfhtml.org/self/2022/jul/28/ruckspeichern-in-argument/1800785#m1800785)...