-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
258 additions
and
156 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# pop over shift | ||
# .pop over .shift | ||
|
||
The `.shift` method removes the first element from an array and returns it. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Empty prototype | ||
|
||
All objects in JavaSCript are instance of `Object` and they inherit properties and methods, such as `.toString`. | ||
|
||
There are situations in which you just want to use an `Object` instance as a container of values. | ||
|
||
For thoese cases, an `Object` maybe be deliberately created with an empty prototype, meaning it inherits nothing. | ||
|
||
```js | ||
const cache = Object.create(null) | ||
``` | ||
|
||
In this way, the object has a smaller memory footprint. | ||
|
||
Since it doesn't inherit from other object, you can't typechecking using `instanceof`: | ||
|
||
```js | ||
Object.create(null) instanceof Object // false | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Global flag (g) | ||
|
||
The global flag indicates the regular expression should be tested against all possible matches in a string. | ||
|
||
```js | ||
const regex = new RegExp('o', 'g') | ||
|
||
const greetings = 'Hello, how are you?' | ||
const results = greetings.match(regex) | ||
|
||
console.log(results.length) // => 3 | ||
``` | ||
|
||
If you just want to know if a string matches a regular expression, you use [RegExp.test()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test) for it. | ||
|
||
```js | ||
const regex = new RegExp('o', 'g') | ||
const greetings = 'Hello, how are you?' | ||
|
||
regex.test(greetings) // => true | ||
``` | ||
|
||
When `.test` is invoked, the regex keeps internally the state of the search at [lastIndex](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) property. | ||
|
||
This will be cause inconsistent results if you call test method several times: | ||
|
||
```js | ||
const regex = new RegExp('o', 'g') | ||
const greetings = 'Hello, how are you?' | ||
|
||
regex.lastIndex // => 0 | ||
regex.test(greetings) // => true | ||
regex.lastIndex // => 5 | ||
regex.test(greetings) // => true | ||
regex.lastIndex // => 9 | ||
regex.test(greetings) // => true | ||
regex.lastIndex // => 17 | ||
regex.test(greetings) // => false | ||
regex.lastIndex // => 0 | ||
``` | ||
|
||
As you can see, `lastIndex` will continue from the last matched result. | ||
|
||
That can be easily avoided just wrapping into a function that will be executed from the beginning every time: | ||
|
||
```js | ||
const regex = new RegExp('o', 'g') | ||
const greetings = 'Hello, how are you?' | ||
|
||
const test = (regex, str) => regex.test(str) | ||
|
||
test(regex, greetings) // => true | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Unicode flag (u) | ||
|
||
The unicode (`u`) flag is mandatory when working with Unicode strings, in particular when you might need to handle characters in astral planes, the ones that are not included in the first 1600 Unicode characters. | ||
|
||
Like Emojis, for example, but not just those. | ||
|
||
If you don’t add that flag, this simple regex that should match one character will not work, because for JavaScript that emoji is represented internally by 2 characters (see [Unicode in JavaScript](https://flaviocopes.com/javascript-unicode/)): | ||
|
||
```js | ||
/^.$/.test('a') // ✅ | ||
/^.$/.test('🐶') // ❌ | ||
/^.$/u.test('🐶') // ✅ | ||
``` | ||
|
||
So, always use the `u` flag. | ||
|
||
Keep in mind that Unicode, just normal characters, handle ranges: | ||
|
||
```js | ||
/[a-z]/.test('a') // ✅ | ||
/[1-9]/.test('1') // ✅ | ||
|
||
/[🐶-🦊]/u.test('🐺') // ✅ | ||
/[🐶-🦊]/u.test('🐛') // ❌ | ||
``` | ||
|
||
JavaScript checks the internal code representation, so `🐶 < 🐺 < 🦊` because `\u1F436 < \u1F43A < \u1F98A`. Check [emoji-regex](https://github.com/mathiasbynens/emoji-regex) for exploring more about that. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
e86b527
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: