1.0.1 • Published 4 years ago

j_simple_parser v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

A simple and easy to use text parsing javascript library, j_simple_parser comes with elegant parsers that you can use to build complex and more elegant parsers.

//Import parsers

const {
    string,
    any,
    char,
    firstChar,
    lastChar,
    remainingChars,
    digit,
    space,
    spaceOptional,
    letter,
    pattern,
    word,
    charsBefore,
    debug,
    newLine,
} = require('../src/j_simple_parser');

//Examples

console.log(string('WE',false).seq(any(space()).star()).parse('Welcome to Magabelab')); //Success { start: 0, end: 7, value: 'Welcome' }

console.log(any(char('g')).plus().parse('I am good')); //Success { start: 0, end: 5, value: 'I am ' }

console.log(digit().plus().firstMatch('I am 20 years old and my sister is 12 years old')); //Success { start: 5, end: 7, value: '20' }

console.log(digit().plus().firstMatch('I am 20 years old and my sister is 12 years old',8)); //Success { start: 35, end: 37, value: '12' }

console.log(digit().removeFrom('I am 20 years old and my sister is 12 years old')); //I am  years old and my sister is  years old

//Capitalize a sentence
console.log(letter().replaceIn('I am 20 years old and my sister is 12 years old',(letter)=>{
     return letter.toUpperCase();
})); //I AM 20 YEARS OLD AND MY SISTER IS 12 YEARS OLD

console.log(letter().allStringMatches('Magabe Lab')); //['M', 'a', 'g','a', 'b', 'e','L', 'a', 'b']


//format space
console.log(space().replaceIn('I    am      good ',(letter)=>{
     return ' ';
}));  // I am good

console.log(firstChar().parse('I am the best')); //Success { start: 0, end: 1, value: 'I' }

console.log(lastChar().allMatches('I am the best')); //Success { start: 12, end: 13, value: 't' } ]

console.log(remainingChars().parse('I am the best')); //Success { start: 0, end: 13, value: 'I am the best' }

console.log(space().seq(char('e').repeat(2, 3)).parse('    eee')); //Success { start: 0, end: 7, value: '    eee' }

console.log(digit().or(space()).removeFrom('  744  eee')); //eee

console.log(char('c').parse('chura', 0)); //Success { start: 0, end: 1, value: 'c' }

console.log(char('c').parse('chura', 1)); //Failure { position: 1, message: '' }

const tag = char('<')
    .seq(letter().plus())
    .seq(spaceOptional())
    .seq(char('/').optional())
    .seq(char('>'));

console.log(tag.firstStringMatch('<tag></tag>')); //<tag>

console.log(tag.allStringMatches('<tag/><i></i>')); //[ '<tag/>', '<i>' ]
1.0.1

4 years ago

1.0.0

4 years ago