123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- {
- class Store {
- #reducer;
- #state;
- #cbs = []
- constructor(reducer){
- this.#reducer = reducer
- this.#state = reducer(undefined, {})
- }
- getState(){
- return this.#state
- }
- subscribe(cb){
- (this.#cbs.push(cb), () => this.#cbs = this.#cbs.filter(c => c !== cb))
- }
- dispatch(action){
- let newState = this.#reducer (this.#state,action)
- if (newState !== this.#state) {
- this.#state = newState
- for ( let cb of this.#cbs) {
- cb ()
- }
- }
- }
- get state () {
- return this.#state
- }
- }
- class StoreThunk extends Store {
- dispatch(action) {
-
- if (typeof action === 'function') {
-
- return action(this.dispatch.bind(this), this.getState.bind(this))
- } else {
- super.dispatch(action)
- }
- }
- }
- }
- class RGB {
- #r;
- #g;
- #b;
- set r (newR) {
- if (typeof newR !="number" || !(newR>= 0 && newR<=255)) {
- throw new RangeError('Неправильный формат или диапазон')
- } else {
- this.#r = newR
- }
- }
- set g (newG) {
- if (typeof newG !="number" || !(newG>= 0 && newG<=255)) {
- throw new RangeError('Ошибка в типе значений')
- } else {
- this.#g = newG
- }
- }
- set b (newB) {
- if (typeof newB != 'number' || !(newB>= 0 && newB<=255)) {
- throw new RangeError('Ошибка в типе значений')
- } else {
- this.#b = newB
- }
- }
- set rgb (newRgb) {
- let matchColors = /rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/;
- let match = matchColors.exec(newRgb)
- if (match != null) {
- this.#r = parseInt(match[1])
- this.#g = parseInt(match[2])
- this.#b = parseInt(match[3])
- return
- } else {
- throw new SyntaxError('Вы пытаетесь интерпретировать синтаксически неправильный код')
- }
- }
- set hex (newHex){
- let matchColors = /^#([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/
- let match = matchColors.exec(newHex)
- if (match !== null) {
- this.#r = parseInt(match[1], 16)
- this.#g = parseInt(match[2], 16)
- this.#b = parseInt(match[3], 16)
- return
- } else {
- throw new SyntaxError('Вы пытаетесь интерпретировать синтаксически неправильный код')
- }
- }
-
- get r (){
- return this.#r
- }
- get g (){
- return this.#g
- }
- get b (){
- return this.#b
- }
- get rgb (){
- return `rgb(${this.#r},${this.#g},${this.#b})`
- }
- get hex (){
- return `#${this.componentToHex(this.#r)}${this.componentToHex(this.#g)}${this.componentToHex(this.#b)}`
- }
- componentToHex(color) {
- let hex = color.toString(16)
- return hex.length == 1 ? "0" + hex : hex
- }
- }
- const rgb = new RGB
- rgb.r = 15
- rgb.g = 128
- rgb.b = 192
- console.log(rgb.hex)
- console.log(rgb.rgb)
- rgb.hex = '#203040'
- console.log(rgb.rgb)
- rgb.rgb = 'rgb(100, 90, 50)'
- console.log(rgb.r, rgb.g, rgb.b)
- rgb.hex = 'дичь'
- rgb.b = 1000
|