NATO Phonetic Text Replacement


#1

Trying to generate an action that replaces letters of text in the current draft and substitutes the NATO phonetic library. There is one part of this in the current Drafts library - but was trying to do this on a stand-alone with an array (as opposed to else… statements).

When I run this - it seems to be tripping up at the end - error message is d.update is not a function? But I use this same d.update() in a number of other drafts, so slightly confused.

Any help / pointers would be appreciated…

I skipped a whole bunch of letters in the array when I posted in the code below.

var d = encodeURIComponent(draft.content);
var lctext=d.toLowerCase();
var phonArray=new Array;
phonArray["a"]="Alpha";
phonArray["b"]="Bravo";
phonArray["c"]="Charlie";
phonArray[" "]="[space]";

var transform="";

for(var i=0;i < lctext.length;i++)
		{
			var thisChar=lctext.charAt(i);
			transform += phonArray[thisChar] + " ";
		}
		
// update content
d.content = transform;
d.update();

#2

Try:

draft.content = transform;
draft.update();

#3

To address the error message, change “d” to “draft”. You have defined “d”as encoded text.


#4

Thanks guys - was banging my head trying to fix this!


#5

You can simplify your lookup table:

const lookup = {
	a: 'Alpha',
	b: 'Bravo',
	c: 'Charlie',
};

If you loop over strings a lot, you can simplify it with:

for (const c of lctext)
{
    transform += lookup[c] + " ";
}

#6

Much cleaner - making those changes now!