Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

day-10 #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ typings/
.yarn-integrity

# next.js build output
.next
.next
package.json
28 changes: 28 additions & 0 deletions js-exercises/better-string-lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## Instructions

You need to create a small string library containing two functions. Your functions should not restrict itself with ASCII-only input strings.

The functions are —

- **reverse(str)** — reverses a given string

```js
reverse("ab");
// 'ba'

reverse("foo 𝌆 bar");
// rab 𝌆 oof

reverse("mañana mañana");
// anañam anañam
```

- **equal(str1, str2)** — returns `true` when the strings are equivalent, `false` otherwise

```js
equal("a", "a");
// true

equal("mañana", "mañana");
// true
```
23 changes: 23 additions & 0 deletions js-exercises/better-string-lib/betterStringLib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

function reverse(string) {
const regexSymbolWithCombiningMarks = /([\0-\u02FF\u0370-\u1DBF\u1E00-\u20CF\u2100-\uD7FF\uDC00-\uFE1F\uFE30-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF])([\u0300-\u036F\u1DC0-\u1DFF\u20D0-\u20FF\uFE20-\uFE2F]+)/g;
const regexSurrogatePair = /([\uD800-\uDBFF])([\uDC00-\uDFFF])/g;
const stringProcess = string.replace(regexSymbolWithCombiningMarks, ($0, $1, $2) => reverse($2) + $1).replace(regexSurrogatePair, '$2$1');

let result = '';
let index = stringProcess.length;
while (index) {
index -= 1;
result += stringProcess.charAt(index);
}
return result;
}

function equal(stringA, stringB) {
return stringA.normalize('NFC') === stringB.normalize('NFC');
}

module.exports = {
equal,
reverse,
};
26 changes: 26 additions & 0 deletions js-exercises/better-string-lib/betterStringLib.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// import { betterStringLib } from './betterStringLib';
const betterStringLib = require('./betterStringLib');

console.log(betterStringLib);
describe('Better String Library', () => {
it('reverse of ab', () => {
expect(betterStringLib.reverse('ab')).toEqual('ba');
});

it('reverse of foo 𝌆 bar', () => {
expect(betterStringLib.reverse('foo 𝌆 bar')).toEqual('rab 𝌆 oof');
});

it('reverse of mañana mañana', () => {
expect(betterStringLib.reverse('mañana mañana')).toEqual('anañam anañam');
});

it('equal of a, a', () => {
expect(betterStringLib.equal('a', 'a')).toEqual(true);
});

it('equal of mañana, mañana', () => {
expect(betterStringLib.equal('mañana', 'mañana')).toEqual(true);
});

});
1 change: 1 addition & 0 deletions js-exercises/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"babel-jest": "^25.1.0",
"jest": "^25.1.0"
},
"eslintIgnore": ["betterStringLib.js"],
"dependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.8.4",
Expand Down