Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
李楚霏
/
refactorTest
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
cd158ad9
authored
5 years ago
by
李楚霏
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
task6--提取函数final
parent
746023e1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
20 deletions
+55
-20
length/src/index.js
+46
-11
length/src/index.test.js
+9
-9
No files found.
length/src/index.js
View file @
cd158ad9
import
*
as
Constants
from
'./constants'
import
{
Constants
}
from
'./constants'
export
class
Length
{
value
unit
...
...
@@ -17,32 +17,67 @@ export class Length {
}
parseTo
(
unitNode
)
{
let
unitLength
=
this
if
(
this
.
unit
===
Constants
.
Constants
.
YARD
)
{
if
(
unitNode
===
Constants
.
Constants
.
FOOT
)
{
if
(
this
.
unit
===
Constants
.
YARD
)
{
if
(
unitNode
===
Constants
.
FOOT
)
{
unitLength
=
new
Length
(
this
.
multipleValue
(
3
),
unitNode
)
}
else
if
(
unitNode
===
Constants
.
Constants
.
INCH
)
{
}
else
if
(
unitNode
===
Constants
.
INCH
)
{
unitLength
=
new
Length
(
this
.
multipleValue
(
36
),
unitNode
)
}
}
if
(
this
.
unit
===
Constants
.
Constants
.
INCH
)
{
if
(
unitNode
===
Constants
.
Constants
.
YARD
)
{
if
(
this
.
unit
===
Constants
.
INCH
)
{
if
(
unitNode
===
Constants
.
YARD
)
{
unitLength
=
new
Length
(
this
.
divisionValue
(
36
),
unitNode
)
}
else
if
(
unitNode
===
Constants
.
Constants
.
FOOT
)
{
}
else
if
(
unitNode
===
Constants
.
FOOT
)
{
unitLength
=
new
Length
(
this
.
value
/
12
,
unitNode
)
}
}
if
(
this
.
unit
===
Constants
.
Constants
.
FOOT
)
{
if
(
unitNode
===
Constants
.
Constants
.
YARD
)
{
if
(
this
.
unit
===
Constants
.
FOOT
)
{
if
(
unitNode
===
Constants
.
YARD
)
{
unitLength
=
new
Length
(
this
.
divisionValue
(
3
),
unitNode
)
}
else
if
(
unitNode
===
Constants
.
Constants
.
INCH
)
{
}
else
if
(
unitNode
===
Constants
.
INCH
)
{
unitLength
=
new
Length
(
this
.
multipleValue
(
12
),
unitNode
)
}
}
return
unitLength
}
as
(
targetUnit
)
{
return
new
Length
(
this
.
_valueIn
(
targetUnit
),
targetUnit
)
}
_valueIn
(
unit
)
{
if
(
this
.
unit
===
Constants
.
YARD
)
{
if
(
unit
===
Constants
.
FOOT
)
{
// this.multipleValue(3)
return
this
.
value
*
3
}
if
(
unit
===
Constants
.
INCH
)
{
// this.multipleValue(36)
return
this
.
value
*
36
}
}
if
(
this
.
unit
===
Constants
.
INCH
)
{
if
(
unit
===
Constants
.
YARD
)
{
// this.divisionValue(3)
return
this
.
value
/
36
}
if
(
unit
===
Constants
.
FOOT
)
{
// this.divisionValue(12)
return
this
.
value
/
12
}
}
if
(
this
.
unit
===
Constants
.
FOOT
)
{
if
(
unit
===
Constants
.
YARD
)
{
// this.divisionValue(3)
return
this
.
value
/
3
}
if
(
unit
===
Constants
.
INCH
)
{
// this.multipleValue(12)
return
this
.
value
*
12
}
}
}
divisionValue
(
conversion_Target_Unit_Value
)
{
return
this
.
value
/
conversion_Target_Unit_Value
...
...
This diff is collapsed.
Click to expand it.
length/src/index.test.js
View file @
cd158ad9
...
...
@@ -10,56 +10,56 @@ describe('Length', () => {
})
it
(
"should 1 'foot' equals 12 inches"
,
()
=>
{
const
result
=
new
Length
(
1
,
Constants
.
FOOT
).
parseTo
(
Constants
.
INCH
)
const
result
=
new
Length
(
1
,
Constants
.
FOOT
).
as
(
Constants
.
INCH
)
//
parseTo(Constants.INCH)
expect
(
result
.
getVal
()).
toEqual
(
12
)
expect
(
result
.
getUint
()).
toEqual
(
Constants
.
INCH
)
})
it
(
"should 3 feet equals 1 yard"
,
()
=>
{
const
result
=
new
Length
(
3
,
Constants
.
FOOT
).
parseTo
(
Constants
.
YARD
)
const
result
=
new
Length
(
3
,
Constants
.
FOOT
).
as
(
Constants
.
YARD
)
//
parseTo(Constants.YARD)
expect
(
result
.
getVal
()).
toEqual
(
1
)
expect
(
result
.
getUint
()).
toEqual
(
Constants
.
YARD
)
})
it
(
"should 1 Constants.YARD equals 3 feet"
,
()
=>
{
const
result
=
new
Length
(
1
,
Constants
.
YARD
).
parseTo
(
Constants
.
FOOT
)
const
result
=
new
Length
(
1
,
Constants
.
YARD
).
as
(
Constants
.
FOOT
)
//
parseTo(Constants.FOOT)
expect
(
result
.
getVal
()).
toEqual
(
3
)
expect
(
result
.
getUint
()).
toEqual
(
Constants
.
FOOT
)
})
it
(
"should 1 Constants.YARD equals 36 inches"
,
()
=>
{
const
result
=
new
Length
(
1
,
Constants
.
YARD
).
parseTo
(
Constants
.
INCH
)
const
result
=
new
Length
(
1
,
Constants
.
YARD
).
as
(
Constants
.
INCH
)
expect
(
result
.
getVal
()).
toEqual
(
36
)
expect
(
result
.
getUint
()).
toEqual
(
Constants
.
INCH
)
})
it
(
"should 1 Constants.YARD equals 1 Constants.YARD"
,
()
=>
{
const
result
=
new
Length
(
1
,
Constants
.
YARD
)
.
parseTo
(
Constants
.
YARD
)
const
result
=
new
Length
(
1
,
Constants
.
YARD
)
expect
(
result
.
getVal
()).
toEqual
(
1
)
expect
(
result
.
getUint
()).
toEqual
(
Constants
.
YARD
)
})
it
(
"should 12 inches equals 1 'foot'"
,
()
=>
{
const
result
=
new
Length
(
12
,
Constants
.
INCH
).
parseTo
(
Constants
.
FOOT
)
const
result
=
new
Length
(
12
,
Constants
.
INCH
).
as
(
Constants
.
FOOT
)
expect
(
result
.
getVal
()).
toEqual
(
1
)
expect
(
result
.
getUint
()).
toEqual
(
Constants
.
FOOT
)
})
it
(
"should 36 inches equals 1 yard"
,
()
=>
{
const
result
=
new
Length
(
36
,
Constants
.
INCH
).
parseTo
(
Constants
.
YARD
)
const
result
=
new
Length
(
36
,
Constants
.
INCH
).
as
(
Constants
.
YARD
)
expect
(
result
.
getVal
()).
toEqual
(
1
)
expect
(
result
.
getUint
()).
toEqual
(
Constants
.
YARD
)
})
it
(
"should 1 inch equals 1 inch"
,
()
=>
{
const
result
=
new
Length
(
1
,
Constants
.
INCH
)
.
parseTo
(
Constants
.
INCH
)
const
result
=
new
Length
(
1
,
Constants
.
INCH
)
expect
(
result
.
getVal
()).
toEqual
(
1
)
expect
(
result
.
getUint
()).
toEqual
(
Constants
.
INCH
)
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment