2018-05-14 16:31:04 +02:00
/ * *
* @ author n1474335 [ n1474335 @ gmail . com ]
* @ copyright Crown Copyright 2016
* @ license Apache - 2.0
* /
2019-07-09 13:23:59 +02:00
import Operation from "../Operation.mjs" ;
2022-06-03 22:41:37 +02:00
import OperationError from "../errors/OperationError.mjs" ;
2018-05-14 16:31:04 +02:00
/ * *
* To Upper case operation
* /
class ToUpperCase extends Operation {
/ * *
* ToUpperCase constructor
* /
constructor ( ) {
super ( ) ;
this . name = "To Upper case" ;
this . module = "Default" ;
this . description = "Converts the input string to upper case, optionally limiting scope to only the first character in each word, sentence or paragraph." ;
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [
{
"name" : "Scope" ,
"type" : "option" ,
"value" : [ "All" , "Word" , "Sentence" , "Paragraph" ]
}
] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
2021-12-29 20:59:48 +01:00
if ( ! args || args . length === 0 ) {
2022-06-03 22:41:37 +02:00
throw new OperationError ( "No capitalization scope was provided." ) ;
2021-12-29 20:59:48 +01:00
}
2022-06-03 22:41:37 +02:00
2018-05-14 16:31:04 +02:00
const scope = args [ 0 ] ;
2022-06-03 22:41:37 +02:00
2021-12-29 20:32:39 +01:00
if ( scope === "All" ) {
return input . toUpperCase ( ) ;
}
2022-06-03 22:41:37 +02:00
2021-12-29 20:32:39 +01:00
const scopeRegex = {
"Word" : /(\b\w)/gi ,
"Sentence" : /(?:\.|^)\s*(\b\w)/gi ,
"Paragraph" : /(?:\n|^)\s*(\b\w)/gi
2022-06-03 22:41:37 +02:00
} [ scope ] ;
if ( scopeRegex === undefined ) {
2021-12-29 20:32:39 +01:00
throw new OperationError ( "Unrecognized capitalization scope" ) ;
2018-05-14 16:31:04 +02:00
}
2022-06-03 22:41:37 +02:00
// Use the regex to capitalize the input
return input . replace ( scopeRegex , function ( m ) {
return m . toUpperCase ( ) ;
} ) ;
2018-05-14 16:31:04 +02:00
}
/ * *
* Highlight To Upper case
*
* @ param { Object [ ] } pos
* @ param { number } pos [ ] . start
* @ param { number } pos [ ] . end
* @ param { Object [ ] } args
* @ returns { Object [ ] } pos
* /
highlight ( pos , args ) {
return pos ;
}
/ * *
* Highlight To Upper case in reverse
*
* @ param { Object [ ] } pos
* @ param { number } pos [ ] . start
* @ param { number } pos [ ] . end
* @ param { Object [ ] } args
* @ returns { Object [ ] } pos
* /
highlightReverse ( pos , args ) {
return pos ;
}
}
export default ToUpperCase ;