Commit afd3bfcb by 李楚霏

task3 --提取字符串常量

parent e02e29ff
export class Length {
value
unit
// unitEnum = ['foot', 'yard', 'inch']
static FOOT = 'foot'
static YARD = 'yard'
static INCH = 'inch'
constructor(val, uint) {
this.value = val
......@@ -10,33 +12,33 @@ export class Length {
getVal() {
return this.value
}
}
getUint() {
return this.unit
}
parseTo(unitNode) {
let unitLength = this
if (this.unit === 'yard') {
if (unitNode === 'f') {
if (this.unit === Length.YARD ) {
if (unitNode === Length.FOOT) {
unitLength = new Length(this.value * 3, unitNode)
} else if (unitNode === 'inch') {
} else if (unitNode === Length.INCH) {
unitLength = new Length(this.value * 36, unitNode)
}
}
if (this.unit === 'inch') {
if (unitNode === 'yard') {
if (this.unit === Length.INCH) {
if (unitNode === Length.YARD) {
unitLength = new Length(this.value / 36, unitNode)
} else if (unitNode === 'f') {
} else if (unitNode === Length.FOOT) {
unitLength = new Length(this.value / 12, unitNode)
}
}
if (this.unit === 'f') {
if (unitNode === 'yard') {
if (this.unit === Length.FOOT) {
if (unitNode === Length.YARD) {
unitLength = new Length(this.value / 3, unitNode)
} else if (unitNode === 'inch') {
} else if (unitNode === Length.INCH) {
unitLength = new Length(this.value * 12, unitNode)
}
}
......
......@@ -2,65 +2,65 @@ import { Length } from './index'
describe('Length', () => {
it("should 1 'foot' equals 1 'foot'", () => {
const length = new Length(1, 'f')
const length = new Length(1, Length.FOOT)
expect(length.getVal()).toEqual(1)
expect(length.getUint()).toEqual('f')
expect(length.getUint()).toEqual(Length.FOOT)
})
it("should 1 'foot' equals 12 inches", () => {
const result = new Length(1, 'f').parseTo('inch')
const result = new Length(1, Length.FOOT).parseTo(Length.INCH)
expect(result.getVal()).toEqual(12)
expect(result.getUint()).toEqual('inch')
expect(result.getUint()).toEqual(Length.INCH)
})
it("should 3 feet equals 1 'yard'", () => {
const result = new Length(3, 'f').parseTo('yard')
it("should 3 feet equals 1 Length.YARD", () => {
const result = new Length(3, Length.FOOT).parseTo(Length.YARD)
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual('yard')
expect(result.getUint()).toEqual(Length.YARD)
})
it("should 1 'yard' equals 3 feet", () => {
const result = new Length(1, 'yard').parseTo('f')
it("should 1 Length.YARD equals 3 feet", () => {
const result = new Length(1, Length.YARD).parseTo(Length.FOOT)
expect(result.getVal()).toEqual(3)
expect(result.getUint()).toEqual('f')
expect(result.getUint()).toEqual(Length.FOOT)
})
it("should 1 'yard' equals 36 inches", () => {
const result = new Length(1, 'yard').parseTo('inch')
it("should 1 Length.YARD equals 36 inches", () => {
const result = new Length(1, Length.YARD).parseTo(Length.INCH)
expect(result.getVal()).toEqual(36)
expect(result.getUint()).toEqual('inch')
expect(result.getUint()).toEqual(Length.INCH)
})
it("should 1 'yard' equals 1 'yard'", () => {
const result = new Length(1, 'yard').parseTo('yard')
it("should 1 Length.YARD equals 1 Length.YARD", () => {
const result = new Length(1, Length.YARD).parseTo(Length.YARD)
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual('yard')
expect(result.getUint()).toEqual(Length.YARD)
})
it("should 12 inches equals 1 'foot'", () => {
const result = new Length(12, 'inch').parseTo('f')
const result = new Length(12, Length.INCH).parseTo(Length.FOOT)
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual('f')
expect(result.getUint()).toEqual(Length.FOOT)
})
it("should 36 inches equals 1 'yard'", () => {
const result = new Length(36, 'inch').parseTo('yard')
it("should 36 inches equals 1 yard", () => {
const result = new Length(36, Length.INCH).parseTo(Length.YARD)
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual('yard')
expect(result.getUint()).toEqual(Length.YARD)
})
it("should 1 inch equals 1 'inch'", () => {
const result = new Length(1, 'inch').parseTo('inch')
it("should 1 inch equals 1 inch", () => {
const result = new Length(1, Length.INCH).parseTo(Length.INCH)
expect(result.getVal()).toEqual(1)
expect(result.getUint()).toEqual('inch')
expect(result.getUint()).toEqual(Length.INCH)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment