[Logo] Jaikoz and SongKong Forums
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Help with some Functions  XML
Forum Index -> SongKong Questions
Author Message
laurie_lewis



Joined: 30/04/2018 21:10:27
Messages: 6
Offline

Hi,

CAUTION; I am not a programmer of any type but I have scraped together some function ideas and I am after some assistance in making sure they are workable before I try them out. I am also after any advice on whether they are good or bad ideas.


1. Remove a specified Character from end of a string. In this case I am imagining doing something like RemoveCharacterAtEnd(title,"."). This would remove all instances of that character from the end of the field nominated.

Code:
function RemoveCharacterAtEnd(value,character) {
 	 while (value.endsWith(character)
    		{
         	value = value.substring(0, value.length() - 1);
     	}
    		value = value.trim()
       	return value;
 }


2. Remove a specified Character from start of a string. In this case I am imagining doing something like RemoveCharacterAtFront(title,"."). This would remove all instances of that character from the front of the field nominated.

Code:
 function RemoveCharacterAtFront(value,character) {
 	 while (value.startsWith(character)
    		{
         	value = value.substring(1, value.length() - 1);
     	}
    		value = value.trim()
       	return value;
 }
 


3. Removing all forms of punctuation from the end of a string. In this case using it like RemovePunctuationAtEnd(title). This will only allow it to end in a character a-z (upper and lower case) or a number.

Code:
 unction RemovePunctuationAtEnd(value) {
 		value = value.replaceAll("[^a-zA-Z0-9]+$", "")
 		return value;
 	
 }
 


4. Removing all forms of punctuation from the start of a string. In this case using it like RemovePunctuationAtStart(title). This will only allow it to start in a character a-z (upper and lower case) or a number.

Code:
 function RemovePunctuationAtStart(value) {
 		value = value.replaceFirst("^[^a-zA-Z0-9]+", "")
 		return value;
 }
 



5. This one I am not sure about at all. I am trying to use a regex expression I found that finds strings that begin with "The" and it will then move it to the end of the string after a comma - ", The". This was an utter guess .

Code:
 function suffixThe(value) {
 	value = value.replaceAll(find /^(?i)(The)\s(.*)$/ replace with "$2, $1")
 	return value;
 	}
 



Laughter at my attempt is allowed.


Laurie
paultaylor

Pro
[Avatar]

Joined: 21/08/2006 09:21:27
Messages: 7556
Offline

They look good, are these to be used as part of your filename mask, if so best thing is to run in Preview mode first and look at the results in the report.

thanks Paul (Administrator)
laurie_lewis



Joined: 30/04/2018 21:10:27
Messages: 6
Offline

I have begun trying out my ideas - the coding anyway.

Function: sufixThe
This is working at this time but I am not sure if it is the best coding.
Works with: ifnotempty(suffixThe(albumartist).substring(0,1),'/') and variations of.

Code:
 function suffixThe(value) {
 if(value.startsWith("The "))
    {
       value = value.substring(4) + ", The"
       return value;
    }
    else
    {
       return value;
    }
 }
 



Function: RemoveCharacterAtEnd
This appears to be working but I am still looking at. it. This will look at the last character of a string and remove it if found. It is looking at multiple characters. In the version shown below it is looking for space, fullstops, commas, _ & -. It should continue to loop through until they are all gone.

Code:
 function RemoveCharacterAtEnd(value) {
   while ((value.endsWith(".")) ||
            (value.endsWith(",")) ||
            (value.endsWith(" ")) ||
            (value.endsWith("-")) ||
            (value.endsWith("_"))) {
       value = value.substring(0, value.length() - 1)
     }
    return value;
 }
 


As I said not sure if it is the best coding but appears to work.

Having fun.

Laurie
laurie_lewis



Joined: 30/04/2018 21:10:27
Messages: 6
Offline

Hi Paul,

The original ones posted don't seem to work but they were a good starting point for me. Made me go back to longer coding but easier to understand. Yes, they are for my filename mask.

Can I just confirm with you about how functions work when nested.

if I have: function3(function2(function1(value))), does it means that function 1 is done on the original value, then function2 on the returned value and finally function3? That is what I am assuming and really when I think about it what I am seeing already.

So if I want and album title changed from:-

The greatest hits...

to

Greatest hits: John Denver, The

Function 1 - Remove ... using my RemoveCharacterAtEnd()
Function 2 - Add ": Album Artist" using RenameGreatest() not written yet
Function 3 - Move The to end using my suffixThe() function

so would read in mask:
Code:
 + ifnotempty(suffixThe(RenameGreatest(RemoveCharacerAtEnd(album))),'/')
 


Another question for you. In your programming of the renaming mask for things like album, artist, title etc do you trim() those values before use? Suppose I am asking should I put a value.trim() into the functions to ensure no leading/following blanks.

Must say I am really liking and impressed with the file naming ability and the great part is that none of the metadata is touched.

Thanks

Laurie
paultaylor

Pro
[Avatar]

Joined: 21/08/2006 09:21:27
Messages: 7556
Offline

Hi Laurie

Yes that look fine to me, note they are just Javascript regular expressions so any standard Javascript reference will be applicable.

Yes we already do trim the values so you don't need to do this.

thanks Paul (Administrator)
 
Forum Index -> SongKong Questions
Go to:   
Powered by JForum 2.1.6 © JForum Team