|
//------------------------------------------------- |
|
|
|
enum Color { |
|
RED, GREEN, BLUE |
|
} |
|
assert.ok(Color.RED instanceof Color); |
|
assert.strictEqual(String(Color.RED), 'Color.RED'); |
|
assert.strictEqual(Color.GREEN.ordinal, 1); |
|
assert.strictEqual(Color.enumValueOf('BLUE'), Color.BLUE); |
|
assert.deepStrictEqual(Color.enumValues, [Color.RED, Color.GREEN, Color.BLUE]); |
|
assert.throws(() => { |
|
// Can’t create new instances |
|
new Color(); |
|
}); |
|
|
|
|
|
//------------------------------------------------- |
|
|
|
// Alas, data properties don’t work, because the enum |
|
// values (TicTacToeColor.X etc.) don’t exist when |
|
// the property definitions are evaluated. |
|
enum TicTacToeColor { |
|
O { |
|
get inverse() { return TicTacToeColor.X } |
|
}, |
|
X { |
|
get inverse() { return TicTacToeColor.O } |
|
}, |
|
} |
|
assert.strictEqual(TicTacToeColor.X.inverse, TicTacToeColor.O); |
|
assert.strictEqual(TicTacToeColor.O.inverse, TicTacToeColor.X); |
|
assert.strictEqual(String(TicTacToeColor.O), 'TicTacToeColor.O'); |
|
assert.strictEqual(TicTacToeColor.O.ordinal, 0); |
|
assert.strictEqual(TicTacToeColor.X.ordinal, 1); |
|
|
|
//------------------------------------------------- |
|
|
|
enum Weekday { |
|
MONDAY, TUESDAY, WEDNESDAY, |
|
THURSDAY, FRIDAY, SATURDAY, SUNDAY; |
|
isBusinessDay() { |
|
switch (this) { |
|
case Weekday.SATURDAY: |
|
case Weekday.SUNDAY: |
|
return false; |
|
default: |
|
return true; |
|
} |
|
} |
|
} |
|
assert.strictEqual(Weekday.SATURDAY.isBusinessDay(), false); |
|
assert.strictEqual(Weekday.MONDAY.isBusinessDay(), true); |
|
|
|
//------------------------------------------------- |
|
|
|
enum Result { |
|
ACCEPTED, REJECTED |
|
} |
|
|
|
enum State { |
|
START { |
|
enter(iter) { |
|
const {value,done} = iter.next(); |
|
if (done) { |
|
return Result.REJECTED; |
|
} |
|
switch (value) { |
|
case 'A': |
|
return State.A_SEQUENCE; |
|
default: |
|
return Result.REJECTED; |
|
} |
|
} |
|
}, |
|
A_SEQUENCE { |
|
enter(iter) { |
|
const {value,done} = iter.next(); |
|
if (done) { |
|
return Result.REJECTED; |
|
} |
|
switch (value) { |
|
case 'A': |
|
return State.A_SEQUENCE; |
|
case 'B': |
|
return State.B_SEQUENCE; |
|
default: |
|
return Result.REJECTED; |
|
} |
|
} |
|
}, |
|
B_SEQUENCE { |
|
enter(iter) { |
|
const {value,done} = iter.next(); |
|
if (done) { |
|
return State.ACCEPT; |
|
} |
|
switch (value) { |
|
case 'B': |
|
return State.B_SEQUENCE; |
|
default: |
|
return Result.REJECTED; |
|
} |
|
} |
|
}, |
|
ACCEPT { |
|
enter(iter) { |
|
return Result.ACCEPTED; |
|
} |
|
}, |
|
} |
|
function runStateMachine(str) { |
|
let iter = str[Symbol.iterator](); |
|
let state = State.START; |
|
while (true) { |
|
state = state.enter(iter); |
|
switch (state) { |
|
case Result.ACCEPTED: |
|
return true; |
|
case Result.REJECTED: |
|
return false; |
|
} |
|
} |
|
} |
|
assert.strictEqual(runStateMachine('AABBB'), true, 'AABBB'); |
|
assert.strictEqual(runStateMachine('AA'), false, 'AA'); |
|
assert.strictEqual(runStateMachine('BBB'), false, 'BBB'); |
|
assert.strictEqual(runStateMachine('AABBC'), false, 'AABBC'); |
|
assert.strictEqual(runStateMachine(''), false, ''); |
|
|
|
//------------------------------------------------- |
|
|
|
enum Mode { |
|
USER_R { |
|
n: 0b100000000, |
|
}, |
|
USER_W { |
|
n: 0b010000000, |
|
}, |
|
USER_X { |
|
n: 0b001000000, |
|
}, |
|
GROUP_R { |
|
n: 0b000100000, |
|
}, |
|
GROUP_W { |
|
n: 0b000010000, |
|
}, |
|
GROUP_X { |
|
n: 0b000001000, |
|
}, |
|
ALL_R { |
|
n: 0b000000100, |
|
}, |
|
ALL_W { |
|
n: 0b000000010, |
|
}, |
|
ALL_X { |
|
n: 0b000000001, |
|
}, |
|
} |
|
assert.strictEqual( |
|
Mode.USER_R.n | Mode.USER_W.n | Mode.USER_X.n | |
|
Mode.GROUP_R.n | Mode.GROUP_X.n | |
|
Mode.ALL_R.n | Mode.ALL_X.n, |
|
0o755); |
|
assert.strictEqual( |
|
Mode.USER_R.n | Mode.USER_W.n | Mode.USER_X.n | Mode.GROUP_R.n, |
|
0o740); |
Hi. Just read the proposal. It looks great. Thanks for good job.
Could I suggest a few points:
About naming. Probably
enumValueOf
logically could befromValue
andfromName
If we provide custom implementation for
valueOf
method then could be possible to avoidordinal
and provide it like default value, or assign new value at all. For example: