Code Monkey home page Code Monkey logo

is-palindrome's Introduction

Palindrome Checker

npm (scoped) devDependencies Status npm bundle size (scoped) npm jsDelivr hits (npm scoped) Dependent repos (via libraries.io), scoped npm package FOSSA Status

Zero dependencies, lightweight and fully functional palindrome checker

Install with npm

npm i @huynhducduy/is-palindrome --save

Install with yarn

yarn add @huynhducduy/is-palindrome

Table of Contents

Usage

CommonJS (Node)

var isPalindrome = require("@huynhducduy/is-palindrome");

ES Modules

import isPalindrome from "@huynhducduy/is-palindrome/dist/esm.js";

Browser (IIFE)

<script
  src="https://cdn.jsdelivr.net/npm/@huynhducduy/is-palindrome@1/dist/iife.js"
  crossorigin="anonymous"
></script>

Example

console.log(
  isPalindrome(
    "    Are we not pure? “No sir!” Panama’s moody Noriega brags. “It is garbage!” Irony dooms a man; a prisoner up to new era.    ",
    {
      remove: ["punctuation", "non-printable-ascii", "whitespace"],
      caseSensitive: false,
      trim: "both",
    }
  )
);
// true

Also support amd (RequireJS), umd, sys (SystemJS) as well.

API

Check if the given string is a valid palindrome

Params

  • str {Any}: String to check, it's not a valid string, the function will try to convert it, or will throws an exception
  • options {Object}: See options object
  • debug {Boolean}: Log debug or not

Return: {Boolean}: True if it is a valid palindrome that match out options, otherwise False

Options pass to is-palindrome. The processing flow is: Normalize -> Remove -> (Transform to lower case) -> Trim (whitespace and trailing)

name type/values default description
exception boolean false Inform the function to throw exceptions or not
normalize boolean false Inform the function to normalize string or not
normalizeForm "NFC", "NFD", "NFKC", "NFKD" "NFC" The form being used to normalize string (if normalize === true), must be one of supported values, otherwise a exception will be thrown or the normalization will be omitted
remove string, [string] [] remove some kind of char from string, accepted: "non-printable-ascii", "punctuation", "whitespace"
caseSensitive boolean true Indicate the case sensitivity of the function
trim "none", "start", "end", "both" "none" Trim trailing whitespace mode
trimTrailing string, [string] Trim trailing characters or strings

Details: in `punctuation` remove mode, these chars will be remove:
~\`!@#\$%^&\*(){}\[\];:"'<,.>?\/\\|\_+=-`

Other awesome projects

  • Update...

Running tests

Install dev dependencies:

yarn && yarn test

Compile & Minify:

yarn compile

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

License

FOSSA Status

is-palindrome's People

Contributors

dependabot[bot] avatar fossabot avatar huynhducduy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

is-palindrome's Issues

Support multiple trimTrailling options

Is your feature request related to a problem? Please describe.
I want to trim multiple trailing strings of the input, which is now not supported

Describe the solution you'd like
Overload trimTrailing options to accept array of strings, and trim them in sequence.

options = {
    trimTrailing: ['a', 'ab', 'c'],
} 

Example

isPalindrome('aaaaabacccccababa', {trimTrailing: ['a', 'ab', 'c']}); // true
// after trim a: 'abacccccababa'
// after trim ab: 'abacccccaba'
// after trim c: 'abacaba'

Alternatives
This is a little bit advanced, but I may want to have mode support for continually trim when any of the string in the array can be matched.
Imagine that a trimming step can make another trailing string happened.

Example:

isPalindrome('abccccccabc', {trimTrailing: ['abc', 'c']})
// Check for trailing 'abc' in 'abccccccabc' but found no occur
// After trim 'c': abcabc
// The string after trim 'c' introduce a trailing 'abc'

Now with this mode we will have something like:

isPalindrome('abccccccabc', {trimTrailing: ['abc', 'c'], trimTrailingCon: true})
// Check for trailing 'abc' in 'abccccccabc' but not found
// After trim 'c': abcabc
// Again, check for trailing 'abc' in 'abcabc' and found, then trim: 'abc'
// Again, check for trailing 'abc' in 'abcabc' and found nothing
// All of the trailing string are not found, so we can stop the operation
// Output: 'abc'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.