1.1.19 • Published 2 years ago

pyyqww_t1 v1.1.19

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Single Selection Model

A model for single selection case

const lsOfOptions = ["a","b","c"]
const model = SingleSelectionModel.NewWithDefault(lsOfOptions) //with an empty default selected item
// const model = SingleSelectionModel.New(lsOfOptions) //with out an empty default selected item
const cur = model.getCur()              // get current selection, must have 1 selection
const empty = model.findEmpty()         // get empty selection, only exists when init with NewWithDefault function
console.log(cur === empty)              // ==> true
const options = model.findOptions()     // get all non-empty option selection
const all = model.getAll()              // all = [options + empty]

const optionA = model.find("a")       // optionA === options[0] === all[1]
console.log(optionA == options[0])
console.log(optionA == all[1])


//OptionA: false, Empty: true
console.log(`OptionA: ${optionA.isSelected()}, Empty: ${empty.isSelected()}`)
optionA.select()                        // select optionA
//OptionA: true, Empty: false
console.log(`OptionA: ${optionA.isSelected()}, Empty: ${empty.isSelected()}`)

ImBusyModule

I'm busy module is simple tool to track busy status.

import {ImBusyModule} from "./ImBusy";

// Create a busy man instance to store his busy status
const busyMan = ImBusyModule.busyMan()

busyMan.amIBusy() // false
const someJob = busyMan.newJob()
busyMan.amIBusy() // true

someJob.doneMyJob() // remove job from busy man

// Busy man finally having time for coding
busyMan.amIBusy() // false 

Add a hook to the BusyMan

const man = busyMan()
let inited = false
let busy = false
man.register(NewSimpleHook(
        it => busy = it,     // status change hook, called when status change
        it => inited = it)   // initialize hook, call when register
)
console.log(`Registered: ${inited}, Busy: ${busy}`) // Registered: true, Busy: false
const j1 = man.newJob()
console.log(`Registered: ${inited}, Busy: ${busy}`) // Registered: true, Busy: true
j1.doneMyJob()
console.log(`Registered: ${inited}, Busy: ${busy}`) // Registered: true, Busy: false

Scope API

Scope API is a set of helper utils for creating object and converting object.

Existence of instance

Scope API considers

  1. any input value of null, undefined as empty.
  2. any input value of non value as non-empty. \ p.s. if not specify the type of, when pass null, the result type will become Scope
import {Scopes, Scope} from "./Scopes";

//----------- Init object

// create empty Scope
let variableOf1: Scope<number> = Scopes.ofNullable<number>(null)
console.log(variableOf1.get()) // => null

// create non-empty scope
let variableOf2: Scope<number> = Scopes.of(1)
console.log(variableOf2.get()) // => 1
let variableO3: number = Scopes.ofElse<number>(null, 3).get()
console.log(variableO3) // => 3
let variable04 = Scopes.ofNullable<number>(null).ifEmpty(4).get()
console.log(variable04) // => 4

Scopes.empty<number>().filterBy((it)=>it === 100).isEmpty() // if scope is empty, always return true
Scopes.of(100).filterBy((it)=>it === 1).isEmpty()           // if scope filter fail, return true
Scopes.of(100).filterBy((it)=>it === 100).isEmpty()         // if scope passed the filter, return false with existing object

// if not specify the type, when passing null, result type is not expected
let variable05: null = Scopes.ofNullable(null).get()
console.log(variable05) // => null

Value initializing or Value converting

Scope API provide apply and map method for

  1. init newly created object
  2. update value by condition
  3. conversion of instance
import {Scope} from "./Scope";

class A {
    public a: number = 1
}

// Init newly create object

class A { public a: number = 1 }

// Init newly create object
let v1: Scope<A> = Scopes.of(new A()).apply(it => it.a = 100) // A.a = 100
let v2: Scope<A> = Scopes.ofNullable<A>(null).apply(it => it.a = 100) // A = null, if null apply statment is not executed
let v3: Scope<A> = Scopes.ofNullable<A>(new A()).ifThenApply(it => it.a == 1, it => it.a = 2) // A.a = 2

// Update value by condition
let v4: Scope<A> = Scopes.ofNullable<A>(new A()).ifThenApply(it => it.a == 0, it => it.a = 2) // A.a = 1


class B {
   private b: number|undefined

   static fromA: (a: A) => B =
           (a: A) => Scopes.of(new B()).apply(b => b.b = a.a).get()
}

// conversion of instance
let v5: Scope<B> = Scopes.ofNullable<A>(new A()).map(it => B.fromA(it)) // B.b = 1

Retrieve object

retrieve the object

  1. get object
  2. get default value if null
let v1: Scope<number> = Scopes.of(1)
let v1Value: number = v1.get() // 1

let v2: Scope<number> = Scopes.ofNullable<A>(null)
let v2Value: number = v2.getOr(2) // 2

Item Transformer

Item Transformer help convert value in clear structure easy understand way. If missing the defaultValue({value}) expression, the transformer throw exception for "No match Found"

import {ItemTransformer} from "./ItemTransformer";

const itemTransfer = ItemTransformer.transfer<number, string>()
        .inCase(it => it === 0).then(it => 0 + "")
        .inCaseValue(1).thenValue("1")
        .defaultValue("2")

console.log(itemTransfer.match(0))             // "0"
console.log(itemTransfer.match(1))             // "1"
console.log(itemTransfer.match(2))             // "2"
console.log(itemTransfer.match(3))             // "2"
1.1.19

2 years ago

1.1.18

2 years ago

1.1.17

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago