Search code examples
javascripttextlorem-ipsum

How to replace text with lorem ipsum text


I have a text that I want to replace with lorem ipsum.

What I mean by that is that I want to be able to provide my text as an input and to get back the exact text length, case match, punctuation marks as an ouptut in a form or lorem ipsum.

For example

I, have a text that I want to replace. END;

might became

D, quam e leme ntum F erme po tincidu. PUR;

I've googled for that library but haven't yet figure out how to adopt that for my use case.

I wonder if you have any ideas how to achieve my goal. What I am trying to achieve is to tun the original text into unreadable text, keeping the formatting, so no one else can guess the original text. So I thought that lorem ipsum might be the way to go.

I was thinking to take each paragraph and based on that paragraph to generate an input for lorem ipsurm library, but seems there is no control over the uppercase/lowercase.


Solution

  • Here is one approach to generate jibberish to match the shape of inputText.

    const jibberRaw = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum';
    const jibberLetters = jibberRaw.toLowerCase().replaceAll(/[^a-z]/g, '');
    function jibberOfShape(shape) {
        return Array.from(shape).map((char, index) => {
          const charIsLetter = char.match(/[a-zA-Z]/);
          if (charIsLetter) {
            const wasUpper = char === char.toUpperCase();
            const jibberLetter = jibberLetters[index % jibberLetters.length];
            return wasUpper
              ? jibberLetter.toUpperCase()
              : jibberLetter;
          } else {
            return char;
          }
        }).join('');
    }
    
    const inputText = 'I, have a text that I want to replace. END;';
    console.log(inputText);
    console.log(jibberOfShape(inputText));