1.0.2 • Published 4 years ago

@pelevesque/is-anagram v1.0.2

Weekly downloads
8
License
MIT
Repository
github
Last release
4 years ago

Build Status Coverage Status JavaScript Style Guide

is-anagram

Checks if a string is an anagram with extra features not seen in other anagram checkers.

Node Repository

https://www.npmjs.com/package/@pelevesque/is-anagram

Installation

npm install @pelevesque/is-anagram

Tests

CommandDescription
npm test or npm run testAll Tests Below
npm run coverStandard Style
npm run standardCoverage
npm run unitUnit Tests

Usage

Parameters

str1    (required)
str2    (required)
options (optional) default = { groupBy: 1, canonicalize: false, substringsToIgnore: [] }

Requiring

const isAnagram = require('@pelevesque/is-anagram')

Basic Usage

const result = isAnagram('elvis', 'lives')
// result === true

Ignoring Certain Substrings

const str1 = '1 elv@@@@is $$$'
const str2 = '$$$ li@@@@ves 1'
const opts = { substringsToIgnore : ['1', '$$$', '@@@@', ' '] }
const result = isAnagram(str1, str2, opts)
// result === true
const str1 = 'christmas tree'
const str2 = 'search, set, trim'
const opts = { substringsToIgnore: [',', ' '] }
const result = isAnagram(str1, str2, opts)
// result === true

Canonicalize Strings

Can be used to make strings case insensitive and remove diacritics, homoglyphs, and graphemes.

const str1 = 'ChristmasTree'
const str2 = 'SearchSetTrim'
const opts = { canonicalize: true }
const result = isAnagram(str1, str2, opts)
// result === true
const str1 = 'AmwÉ'
const str2 = 'arnwe'
const opts = { canonicalize: true }
const result = isAnagram(str1, str2, opts)
// result === true

Grouping by Length

String elements become grouped by a certain length.

const str1 = 'aabbcc'
const str2 = 'abcabc'
const opts = { groupBy: 2 }
const result = isAnagram(str1, str2, opts)
// result === false
const str1 = 'aabbcc'
const str2 = 'bbaacc'
const opts = { groupBy: 2 }
const result = isAnagram(str1, str2, opts)
// result === true

Leftover characters like d are kept in the anagram.

const str1 = 'aabbccd'
const str2 = 'bbdaacc'
const opts = { groupBy: 2 }
const result = isAnagram(str1, str2, opts)
// result === true

Explicit Grouping

You can specify each element of the anagram by using explicit grouping.

const str1 = '22boy$$a'
const str2 = '2b2yo$a$'
const opts = { groupBy: ['22', 'boy', '$$', 'a'] }
const result = isAnagram(str1, str2, opts)
// result === false
const str1 = '22boy$$a'
const str2 = '$$aboy22'
const opts = { groupBy: ['22', 'boy', '$$', 'a'] }
const result = isAnagram(str1, str2, opts)
// result === true
const str1 = '1212'
const str2 = '2121'
const opts = { groupBy: ['1', '2', '12'] }
const result = isAnagram(str1, str2, opts)
// result === false
const str1 = '1212'
const str2 = '1221'
const opts = { groupBy: ['1', '2', '12'] }
const result = isAnagram(str1, str2, opts)
// result === true

Mixtures

const str1 = ' AmÉ23 45$$'
const str2 = '2345n$$ e$$ar '
const opts = {
  groupBy: ['ar', 'n', 'e', '2345'],
  substringsToIgnore: [' ', '$$'],
  canonicalize: true
}
const result = isAnagram(str1, str2, opts)
// result === true