Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maxk096 committed Sep 1, 2020
0 parents commit 58452fe
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"useTabs": false,
"tabWidth": 2,
"printWidth": 100,
"singleQuote": true,
"semi": false,
"arrowParens": "avoid",
"bracketSpacing": true
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020-present Max Kanaradze

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# swap-char

Swaps two chars in a string by indexes.

[![License][license-image]][license-url] [![Downloads][downloads-image]][downloads-url]

## Install

Install using [npm](https://www.npmjs.com/):

```sh
npm install --save swap-char
```

or using [yarn](https://yarnpkg.com/)

```sh
yarn add swap-char
```

## API

### swapChar(str: string, from: number, to: number)

Swaps chars on `from` and `to` indexes and returns a new string.

**`str: string`**

A string to perform a char swap on.

**`from: number`**

The zero-based index from where to take a char. If negative, it is treated as `str.length + from`. (For example, if `from` is `-3` it is treated as `str.length - 3`).

If `from` is greater than or equal to `str.length`, `swapChar()` returns `str` without any changes.

**`to: number`**

The zero-based index at which to put a char. If negative, it is treated as `str.length + to`. (For example, if `to` is `-3` it is treated as `str.length - 3`).

If `to` is greater than or equal to `str.length`, `swapChar()` returns `str` without any changes.

## Examples

```js
import swapChar from 'swap-char';

swapChar('abc', 0, 1); // <~ 'bac'
swapChar('abc', 2, 0); // <~ 'cba'

// with negative indexes
swapChar('abc', 0, -1); // <~ 'cba'
swapChar('abc', -2, -1); // <~ 'acb'

// with incorrect indexes - returns the same string
swapChar('abc', 50, 0); // <~ 'abc'
swapChar('abc', 2, Infinity); // <~ 'abc'

```

#### Author

**Max Kanaradze**

[GitHub Profile](https://github.com/null096)

#### MIT Licensed

[license-image]: http://img.shields.io/npm/l/swap-char.svg
[license-url]: LICENSE

[downloads-image]: https://img.shields.io/npm/dm/swap-char
[downloads-url]: http://npm-stat.com/charts.html?package=swap-char
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module 'swap-char' {
const swapChar: (str: string, from: number, to: number) => string
export default swapChar
}
40 changes: 40 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function isValidIndex(index, strLength) {
return index >= 0 && index < strLength && !isNaN(index) && isFinite(index)
}

function swapChar(str, from, to) {
if (str === undefined || str === null) {
return ''
}

str = String(str)
from = Math.floor(from)
to = Math.floor(to)
const strLength = str.length
if (from < 0) {
from = strLength + from
}
if (to < 0) {
to = strLength + to
}

if (!isValidIndex(from, strLength) || !isValidIndex(to, strLength)) {
return str
}

if (from === to) {
return str
}
const min = Math.min(from, to)
const max = Math.max(from, to)
// prettier-ignore
const newStr =
str.slice(0, min) +
str[max] +
str.slice(min + 1, max) +
str[min] +
str.slice(max + 1)
return newStr
}

module.exports = swapChar
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "swap-char",
"version": "1.0.0",
"description": "Swaps two chars in a string by indexes",
"main": "./index.js",
"types": "./index.d.ts",
"scripts": {
"test": "ava",
"test:watch": "ava --watch",
"prepublishOnly": "npm run test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/null096/swap-char.git"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"swap",
"char",
"string",
"index",
"indexes"
],
"author": "Max Kanaradze",
"license": "MIT",
"bugs": {
"url": "https://github.com/null096/swap-char/issues"
},
"homepage": "https://github.com/null096/swap-char#readme",
"devDependencies": {
"ava": "^3.12.1"
}
}
53 changes: 53 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const test = require('ava')
const swapChar = require('./index')

test('Should swap two chars in a string', t => {
t.is(swapChar('123', 0, 0), '123')
t.is(swapChar('123', 0, 1), '213')
t.is(swapChar('123', 0, 2), '321')
t.is(swapChar('123', 1, 0), '213')
t.is(swapChar('123', 1, 1), '123')
t.is(swapChar('123', 1, 2), '132')
t.is(swapChar('123', 2, 0), '321')
t.is(swapChar('123', 2, 1), '132')
t.is(swapChar('123', 2, 2), '123')
})

test('Should swap two chars in a string with negative indexes', t => {
t.is(swapChar('123', -1, 0), '321')
t.is(swapChar('123', -1, 1), '132')
t.is(swapChar('123', -1, 2), '123')
t.is(swapChar('123', -2, 0), '213')
t.is(swapChar('123', -2, 1), '123')
t.is(swapChar('123', -2, 2), '132')
t.is(swapChar('123', -3, 0), '123')
t.is(swapChar('123', -3, 1), '213')
t.is(swapChar('123', -3, 2), '321')

t.is(swapChar('123', 0, -1), '321')
t.is(swapChar('123', 1, -1), '132')
t.is(swapChar('123', 2, -3), '321')

t.is(swapChar('123', -2, -1), '132')
t.is(swapChar('123', -3, -2), '213')
t.is(swapChar('123', -1, -3), '321')
})

test('Shoud handle edge cases properly', t => {
t.is(swapChar('123', 5, 0), '123')
t.is(swapChar('123', 0, 5), '123')
t.is(swapChar('123', 5, 999), '123')
t.is(swapChar('123', -99, -99), '123')
t.throws(() => swapChar('123', Symbol(1), 0))
t.is(swapChar([1, 2], 0, 1), ',12')
t.is(swapChar('123', null, undefined), '123')
t.is(swapChar('123', '1', '2'), '132')
t.is(swapChar('123', 0.5, -1), '321')
t.is(swapChar('123', /test/, -1), '123')
t.is(swapChar('123', '', ''), '123')
t.is(swapChar('123', '-1', '-2'), '132')
t.is(swapChar('123', ' ', ' '), '123')
t.is(swapChar(null), '')
t.is(swapChar(undefined), '')
t.is(swapChar(), '')
})

0 comments on commit 58452fe

Please sign in to comment.