@@ -14,31 +14,45 @@ const StringSchema = new mongoose.Schema({
1414 properties : {
1515 length : Number ,
1616 is_palindrome : Boolean ,
17- // unique_characters: Number,
17+ unique_characters : Number ,
1818 word_count : Number ,
19- sha256_hash : String , // TODO
20- // character_frequency_map: Object,
19+ sha256_hash : String ,
20+ character_frequency_map : Object ,
2121 } ,
2222 created_at : {
2323 type : Date ,
2424 default : Date . now ,
2525 } ,
2626} ) ;
2727
28- StringSchema . pre ( "validate" , async function ( ) {
28+ StringSchema . pre ( "validate" , function ( ) {
2929 this . _id = crypto . createHash ( "sha256" ) . update ( this . value ) . digest ( "hex" ) ;
3030} ) ;
3131
3232StringSchema . pre ( "save" , async function ( ) {
33+ const counts = this . characterFrequencyMap ( ) ;
34+ const unique_characters = Object . keys ( counts ) . length ;
35+
3336 this . properties = {
3437 length : this . value . length ,
3538 is_palindrome : this . value === this . value . split ( "" ) . reverse ( ) . join ( "" ) ,
39+ unique_characters,
3640 word_count : this . value . split ( " " ) . length ,
3741 sha256_hash : this . _id ,
42+ character_frequency_map : counts ,
3843 } ;
3944} ) ;
4045
41- StringSchema . methods . characterFrequencyMap = function async ( ) { } ;
46+ StringSchema . methods . characterFrequencyMap = function ( ) {
47+ const counts = { } ;
48+ const chars = this . value . match ( / [ a - z ] / g) ;
49+
50+ for ( const char of chars ) {
51+ counts [ char ] = ( counts [ char ] || 0 ) + 1 ;
52+ }
53+
54+ return counts ;
55+ } ;
4256
4357StringSchema . set ( "toJSON" , {
4458 transform : ( _ , ret ) => {
0 commit comments