Skip to content

Commit c7b0c90

Browse files
authored
Merge pull request #1 from lppjunior/develop
Start pattern project and add Observer pattern implementation
2 parents 0644cbd + 091eb39 commit c7b0c90

14 files changed

+176
-37
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
# Changelog
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
## [1.0.0] - 2020-07-29
11+
### Added
12+
- Project conceived by: [@lppjunior](https://github.com/lppjunior).
13+
- Make project structure
14+
- Added jest unity test
15+
- Added standard validation
16+
- Added webpack
17+
- Added Observer pattern

README.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,62 @@
1-
# pattern-js
1+
# pattern-js
2+
3+
Pattern js was created to supply the need to centralize all types of patterns that i will use in my projects.
4+
5+
See many characteristics of this library:
6+
- All pattern hav 100% coverage unity tests and confiability.
7+
- Use 100% native javascript to write this code
8+
9+
| Available Patterns |
10+
|----------------------|
11+
| [Observer](Observer) |
12+
13+
## Observer
14+
The instance (or model) maintains a collection of objects (observers) and will notify them of any changes in their state.
15+
16+
**Class structure**
17+
```js
18+
class Observer() {
19+
on(event: string, callback: function)
20+
emit(event: string, data: object)
21+
}
22+
```
23+
24+
## Example
25+
26+
```js
27+
const observer = new Observer()
28+
29+
observer.on('test', (data) => console.log(data)) // result: Observer emit successful
30+
observer.on('test', (data) => console.log('Test 2 > ', data)) // result: Test 2 > Observer emit successful
31+
32+
observer.emit('test', { status: 'Observer emit successful' })
33+
```
34+
35+
## Versioning
36+
37+
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/lppjunior/pattern-js/tags).
38+
39+
## Authors
40+
41+
* **Luiz Paulo** - *Total project development* - [Github](https://github.com/lppjunior) | [Site](http://lppjunior.com) | [Instagram](https://instagram.com/lppjunior)
42+
43+
## Project Updates
44+
45+
See the [CHANGELOG](CHANGELOG.md) for all version update details
46+
47+
## References
48+
49+
Here are some documents that can help you:
50+
51+
* [Node.js](https://nodejs.org/en/)
52+
* [NPM](https://www.npmjs.com/)
53+
* [ES6](http://es6-features.org/)
54+
* [standard](https://standardjs.com/)
55+
* [Eslint](https://eslint.org/)
56+
* [jest](https://jestjs.io/)
57+
* [babel](https://babeljs.io/)
58+
* [webpack](https://webpack.js.org/)
59+
60+
## License
61+
62+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

dist/favicon.png

552 Bytes
Loading

dist/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!doctype html><html><head><meta charset="utf-8"><meta http-equiv="Content-type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1"><title>Pattern JS</title><link rel="icon" href="favicon.png"></head><body><h1>Patter JS</h1><p>Open you inspection to see result</p><script src="main.js"></script><script src="patterns.min.js"></script></body></html>

dist/main.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function runExample () {
2+
testObserver()
3+
}
4+
5+
function testObserver () {
6+
const observer = new Observer()
7+
observer.on('test', (data) => console.log(data))
8+
observer.on('test', (data) => console.log('Test 2 > ', data))
9+
observer.emit('test', { status: 'Observer emit successful' })
10+
}
11+
12+
document.addEventListener('DOMContentLoaded', runExample, false)

dist/patterns.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/patterns.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-example/favicon.png

552 Bytes
Loading

src-example/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
6+
<meta name="viewport" content="width=device-width,initial-scale=1">
7+
8+
<title><%= htmlWebpackPlugin.options.title %></title>
9+
</head>
10+
<body>
11+
<h1>Patter JS</h1>
12+
<p>Open you inspection to see result</p>
13+
14+
<script src="main.js"></script>
15+
</body>
16+
</html>

src-example/main.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function runExample () {
2+
testObserver()
3+
}
4+
5+
function testObserver () {
6+
const observer = new Observer()
7+
observer.on('test', (data) => console.log(data))
8+
observer.on('test', (data) => console.log('Test 2 > ', data))
9+
observer.emit('test', { status: 'Observer emit successful' })
10+
}
11+
12+
document.addEventListener('DOMContentLoaded', runExample, false)

src/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Observer from './pattern/observer/Observer.js'
2+
3+
export {
4+
Observer
5+
}

src/pattern/observer/Observer.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Observer {
2+
constructor () {
3+
this.observer = []
4+
}
5+
6+
on (event, callback) {
7+
if (this.observer[event] === undefined) {
8+
this.observer[event] = []
9+
}
10+
11+
this.observer[event].push(callback)
12+
13+
return this
14+
}
15+
16+
emit (event, data) {
17+
this.observer[event].forEach(callback => callback(data))
18+
}
19+
}
20+
21+
export default Observer
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Observer from '../Observer.js'
2+
3+
4+
describe('Test Observer', () => {
5+
6+
test('should assert result is OK', () => {
7+
const observer = new Observer()
8+
observer.on('jest-test-event', (data) => {
9+
expect(data).toEqual('OK')
10+
})
11+
observer.emit('jest-test-event', 'OK')
12+
})
13+
14+
test('should assert observer.emit be called', () => {
15+
const observer2 = new Observer()
16+
17+
jest.spyOn(observer2, 'emit')
18+
19+
observer2.on('jest-test-event', () => {})
20+
observer2.on('jest-test-event', () => {})
21+
observer2.emit('jest-test-event', 'OK')
22+
23+
expect(observer2.emit).toHaveBeenCalled()
24+
})
25+
})

0 commit comments

Comments
 (0)