@@ -150,9 +150,9 @@ js-cool provides **140+ utility functions** organized into **16 categories**:
150150
151151| Category | Description | Functions |
152152| ----------------- | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
153- | ** String** | String manipulation | ` camel2Dash ` , ` dash2Camel ` , ` upperFirst ` , ` lowerFirst ` , ` capitalize ` , ` kebabCase ` , ` snakeCase ` , ` truncate ` , ` clearHtml ` , ` clearAttr ` , ` cutCHSString ` , ` getCHSLength ` , ` mapTemplate ` , ` escape ` , ` unescape ` |
153+ | ** String** | String manipulation | ` camel2Dash ` , ` dash2Camel ` , ` upperFirst ` , ` lowerFirst ` , ` capitalize ` , ` kebabCase ` , ` snakeCase ` , ` truncate ` , ` clearHtml ` , ` clearAttr ` , ` cutCHSString ` , ` getCHSLength ` , ` mapTemplate ` , ` template ` , ` words ` , ` escape ` , ` unescape ` |
154154| ** Array** | Array processing | ` unique ` , ` shuffle ` , ` sorter ` , ` sortPinyin ` , ` chunk ` , ` flatten ` , ` groupBy ` , ` keyBy ` , ` countBy ` , ` sample ` , ` sampleSize ` , ` intersect ` , ` intersectionBy ` , ` union ` , ` unionBy ` , ` differenceBy ` , ` minus ` , ` complement ` , ` contains ` , ` all ` , ` any ` , ` searchObject ` , ` drop ` , ` dropRight ` , ` take ` , ` takeRight ` , ` findIndex ` , ` findLastIndex ` , ` zip ` , ` unzip ` |
155- | ** Object** | Object manipulation | ` clone ` , ` extend ` , ` getProperty ` , ` setProperty ` , ` omit ` , ` pick ` , ` cleanData ` , ` safeParse ` , ` safeStringify ` , ` mapKeys ` , ` mapValues ` , ` invert ` , ` arrayToCSV ` , ` CSVToArray ` |
155+ | ** Object** | Object manipulation | ` clone ` , ` extend ` , ` getProperty ` , ` setProperty ` , ` omit ` , ` pick ` , ` cleanData ` , ` safeParse ` , ` safeStringify ` , ` mapKeys ` , ` mapValues ` , ` invert ` , ` mergeWith ` , ` transform ` , ` arrayToCSV ` , ` CSVToArray ` |
156156| ** Type Check** | Type checking | ` getType ` , ` isArray ` , ` isObject ` , ` isPlainObject ` , ` isDate ` , ` isRegExp ` , ` isWindow ` , ` isIterable ` , ` isEqual ` , ` isEmpty ` , ` isNil ` |
157157| ** Validate** | Validation functions | ` isEmail ` , ` isPhone ` , ` isURL ` , ` isIDCard ` , ` isCreditCard ` |
158158| ** URL & Browser** | URL parsing and browser detection | ` getUrlParams ` , ` getUrlParam ` , ` parseUrlParam ` , ` spliceUrlParam ` , ` getDirParams ` , ` ua ` , ` appVersion ` , ` browserVersion ` , ` compareVersion ` , ` nextVersion ` |
@@ -586,6 +586,64 @@ cutCHSString('abc', 10) // 'abc'
586586cutCHSString (' abc' , 10 , true ) // 'abc'
587587```
588588
589+ #### words
590+
591+ Split string into an array of words.
592+
593+ ``` js
594+ import { words } from ' js-cool'
595+
596+ // Default: split by word boundaries
597+ words (' fred, barney, & pebbles' ) // ['fred', 'barney', 'pebbles']
598+ words (' camelCaseHTML' ) // ['camel', 'Case', 'HTML']
599+ words (' PascalCase' ) // ['Pascal', 'Case']
600+ words (' snake_case_string' ) // ['snake', 'case', 'string']
601+ words (' kebab-case-string' ) // ['kebab', 'case', 'string']
602+
603+ // With custom pattern
604+ words (' camelCaseHTML' , / [A-Z ] {2,} / g ) // ['HTML']
605+ words (' hello world' , / \w + / g ) // ['hello', 'world']
606+
607+ // Handle numbers
608+ words (' version2Update' ) // ['version', '2', 'Update']
609+
610+ // Consecutive uppercase
611+ words (' HTMLParser' ) // ['HTML', 'Parser']
612+ ```
613+
614+ #### template
615+
616+ Simple template engine with variable interpolation.
617+
618+ ``` js
619+ import { template } from ' js-cool'
620+
621+ // Basic usage
622+ const compiled = template (' Hello, {{ name }}!' )
623+ compiled ({ name: ' World' }) // 'Hello, World!'
624+
625+ // HTML escaping (default)
626+ const safe = template (' {{ content }}' )
627+ safe ({ content: ' <script>alert("xss")</script>' })
628+ // '<script>alert("xss")</script>'
629+
630+ // Raw output (triple braces)
631+ const raw = template (' {{{ html }}}' )
632+ raw ({ html: ' <strong>bold</strong>' }) // '<strong>bold</strong>'
633+
634+ // Nested properties
635+ const nested = template (' {{ user.name }} is {{ user.age }} years old.' )
636+ nested ({ user: { name: ' John' , age: 30 } }) // 'John is 30 years old.'
637+
638+ // Custom delimiters
639+ const custom = template (' Hello, ${ name }!' , { open: ' ${' , close: ' }' })
640+ custom ({ name: ' World' }) // 'Hello, World!'
641+
642+ // Multiple variables
643+ const multi = template (' {{ a }} and {{ b }} and {{ c }}' )
644+ multi ({ a: 1 , b: 2 , c: 3 }) // '1 and 2 and 3'
645+ ```
646+
589647---
590648
591649### Array
@@ -984,6 +1042,60 @@ invert({ a: 1, b: 2, c: 1 }) // { '1': 'c', '2': 'b' } (duplicate values: last w
9841042invert ({ x: ' apple' , y: ' banana' }) // { apple: 'x', banana: 'y' }
9851043```
9861044
1045+ #### mergeWith
1046+
1047+ Merge objects with custom strategy function.
1048+
1049+ ``` js
1050+ import { mergeWith } from ' js-cool'
1051+
1052+ // Custom array merge (concat instead of replace)
1053+ mergeWith ({ a: [1 , 2 ] }, { a: [3 , 4 ] }, (objValue , srcValue ) => {
1054+ if (Array .isArray (objValue)) {
1055+ return objValue .concat (srcValue)
1056+ }
1057+ })
1058+ // { a: [1, 2, 3, 4] }
1059+
1060+ // Skip certain properties
1061+ mergeWith ({ a: 1 , b: 2 }, { a: 10 , b: 20 }, (objValue , srcValue , key ) => {
1062+ if (key === ' b' ) return objValue // keep original
1063+ })
1064+ // { a: 10, b: 2 }
1065+
1066+ // Merge multiple objects
1067+ mergeWith ({ a: 1 }, { b: 2 }, { c: 3 }, (objValue , srcValue ) => srcValue ?? objValue)
1068+ // { a: 1, b: 2, c: 3 }
1069+ ` ` `
1070+
1071+ #### transform
1072+
1073+ Transform object or array with custom accumulator.
1074+
1075+ ` ` ` js
1076+ import { transform } from ' js-cool'
1077+
1078+ // Transform object to array
1079+ transform ({ a: 1 , b: 2 , c: 3 }, (result , value , key ) => {
1080+ result .push ({ key, value })
1081+ return result
1082+ }, [])
1083+ // [{ key: 'a', value: 1 }, { key: 'b', value: 2 }, { key: 'c', value: 3 }]
1084+
1085+ // Filter and transform
1086+ transform ({ a: 1 , b: 2 , c: 3 , d: 4 }, (result , value , key ) => {
1087+ if (value % 2 === 0 ) result[key] = value * 2
1088+ }, {})
1089+ // { b: 4, d: 8 }
1090+
1091+ // Early exit by returning false
1092+ transform ({ a: 1 , b: 2 , c: 3 }, (result , value , key ) => {
1093+ result[key] = value
1094+ if (key === ' b' ) return false
1095+ }, {})
1096+ // { a: 1, b: 2 }
1097+ ` ` `
1098+
9871099#### searchObject
9881100
9891101Deep search in object tree.
0 commit comments