Skip to content

Commit 60cc20c

Browse files
author
Rajesh Muppalla
authored
Merge pull request #12 from indix/mpn
Basic MPN standardization
2 parents 5f0ac20 + 43bc504 commit 60cc20c

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.indix.utils.core
2+
3+
import org.apache.commons.lang3.StringUtils
4+
import org.apache.commons.lang3.text.WordUtils
5+
6+
object MPN {
7+
// Some domain specific keywords known to be invalid
8+
val BlackListedMpns = Set("unknown", "none", "does not apply", "non applicable", "na", "n/a", "various")
9+
10+
val StopChars = Set(' ', '-', '_', '.', '/')
11+
val TerminateChars = Set(',', '"', '*', '%')
12+
13+
val MaxLen = 50
14+
val MinLen = 3
15+
16+
def isValidIdentifier(input: String): Boolean = {
17+
input match {
18+
case _ if StringUtils.isBlank(input) || input.length > MaxLen || input.length < MinLen => false
19+
case _ if input.count(c => TerminateChars.contains(c)) > 1 => false
20+
case _ if BlackListedMpns.contains(input.toLowerCase) => false
21+
case _ if isTitleCase(input) => false
22+
// Unicode strings yet to be handled
23+
case _ => true
24+
}
25+
}
26+
27+
// Does not consider numbers or one word strings as title-case phrase
28+
def isTitleCase(str: String): Boolean = {
29+
val words = str.split(' ').filter(_.nonEmpty)
30+
if (words.length < 2) false
31+
else words.forall(w => w == WordUtils.capitalizeFully(w) && !StringUtils.isNumeric(w))
32+
}
33+
34+
def standardizeMPN(input: String): Option[String] = {
35+
if (isValidIdentifier(input)) {
36+
Some(input)
37+
} else if (StringUtils.isBlank(input)) {
38+
None
39+
} else if (input.indexWhere(c => TerminateChars.contains(c)) > 0) {
40+
Some(input.substring(0, input.indexWhere(c => TerminateChars.contains(c))))
41+
}
42+
else None
43+
}
44+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.indix.utils.core
2+
3+
import org.scalatest.{FlatSpec, Matchers}
4+
5+
class MPNSpec extends FlatSpec with Matchers {
6+
7+
behavior of "MPN"
8+
9+
it should "check title case" in {
10+
MPN.isTitleCase("Key Shell") should be(true)
11+
MPN.isTitleCase("Samsung Galaxy A8") should be(true)
12+
MPN.isTitleCase("Tempered Glass") should be(true)
13+
14+
MPN.isTitleCase("1442820G1") should be(false)
15+
MPN.isTitleCase("Macbook") should be(false)
16+
MPN.isTitleCase("CE 7200") should be(false)
17+
}
18+
19+
it should "validate identifier" in {
20+
MPN.isValidIdentifier(null) should be (false)
21+
MPN.isValidIdentifier("") should be (false)
22+
MPN.isValidIdentifier("51") should be (false)
23+
MPN.isValidIdentifier("NA") should be (false)
24+
MPN.isValidIdentifier("Does not apply") should be (false)
25+
26+
27+
MPN.isValidIdentifier("DT.VFGAA.003") should be (true)
28+
MPN.isValidIdentifier("A55BM-A/USB3") should be (true)
29+
MPN.isValidIdentifier("cASSP1598345-10") should be (true)
30+
MPN.isValidIdentifier("016393B119058-Regular-18x30-BE-BK") should be (true)
31+
MPN.isValidIdentifier("PJS2V") should be (true)
32+
}
33+
34+
it should "standardize MPN" in {
35+
MPN.standardizeMPN(null) should be (None)
36+
MPN.standardizeMPN("Does not apply") should be (None)
37+
MPN.standardizeMPN("PJS2V") should be (Some("PJS2V"))
38+
39+
MPN.standardizeMPN("105200010437-07-70% All Windows") should be (Some("105200010437-07-70"))
40+
MPN.standardizeMPN("30634190, 30753839, 31253006") should be (Some("30634190"))
41+
}
42+
43+
}

0 commit comments

Comments
 (0)