Skip to content

Commit fc3c402

Browse files
Add truncate function like lodash.truncate
1 parent 0051c14 commit fc3c402

File tree

8 files changed

+70
-14
lines changed

8 files changed

+70
-14
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Using CDN
4949
| `fontConvert` | `content: String(require)`,<br>`targetFontType: fontName(require)`,<br>`orignalFontType: fontName(optional)`| `String` | Converting font to target font type. This method need spelling fix, so it gonna use **spellingFix** in default. **convertFrom** will be detect by **fontDetect** when you don't described.<hr> `fontName` must be one of `unicode` or `zawgyi`. |
5050
| `syllBreak` | `content: String(require)`,<br>`fontType: fontName(optional)`,<br>`breakPoint: String(optional)` | `String` |To make systematic word break of Myanmar text. convertFrom will be detect by fontDetect when you don't described.<hr> `fontName` must be one of `unicode` or `zawgyi`. |
5151
| `spellingFix` | `content: String(require)`,<br>`fontType: fontName(optional)` | `String` | **convertFrom** will be detect by **fontDetect** when you don't described. It fix spelling on Myanmar Text.<hr> `fontName` must be one of `unicode` or `zawgyi`. |
52+
| `truncate` | `content: String(requre)`,<br>`options: Object` | `String` | Like lodash.truncate, it truncate word syllable and space. Default truncate length is 30 and you can change it in `options.length` |
5253

5354
## Usage
5455

@@ -84,6 +85,16 @@ knayi.syllBreak('မင်္ဂလာပါ', null, '$$') // 'မင်္ဂ
8485
knayi.spellingFix('မင်္ဂလာာပါါ') // 'မင်္ဂလာပါ'
8586
```
8687

88+
- **truncate(content [, options])**
89+
```javascript
90+
knayi.truncate('အာယုဝဍ်ဎနဆေးညွှန်းစာကို ဇလွန်ဈေးဘေးဗာဒံပင်ထက် အဓိဋ္ဌာန်လျက် ဂဃနဏဖတ်ခဲ့သည်။', { length: 30, omission: '...' });
91+
// "အာယုဝဍ်ဎနဆေးညွှန်းစာကို ဈေး..."
92+
```
93+
**options of truncate**
94+
- `length: Number` default is 30
95+
- `omission:String` default is '...'
96+
- `fontType: String` it automatically detect if it not specified
97+
8798
## Using googlei18n/myanmartools in detector.js
8899

89100
Now you can now use `googlei18n/myanmartools` library in detector.

dist/knayi-myscript.es.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/knayi-myscript.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/knayi-myscript.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/knayi-myscript.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/knayi-myscript.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

library/truncate.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const fontDetect = require('./detector');
2+
const syllBreak = require('./syllBreak');
3+
const globalOptions = require('./globalOptions');
4+
5+
const mmCharacterRange = /[\u1000-\u109F]/;
6+
7+
function truncate(content, options) {
8+
options = options || {};
9+
var fontType = options.fontType;
10+
var length = options.length || 30;
11+
var omission = options.omission || '...';
12+
13+
var absoulteLength = length - omission.length;
14+
15+
if (content === '' || !mmCharacterRange.test(content))
16+
return content.substr(0, absoulteLength) + omission;
17+
18+
if (!fontType)
19+
fontType = fontDetect(content);
20+
21+
var syllables = syllBreak(content, fontType).split(/\u200B/);
22+
23+
return syllables.reduce(function (curr, syll) {
24+
var left = absoulteLength - curr.length;
25+
if (left > 0) {
26+
if (syll.length <= left) {
27+
curr += syll;
28+
} else {
29+
var spaceBreak = syll.split(/\s/);
30+
31+
curr += spaceBreak.reduce(function (_curr, word) {
32+
if (word.length + 1 <= left - _curr.length) {
33+
_curr += word + ' ';
34+
}
35+
return _curr;
36+
}, '')
37+
}
38+
}
39+
return curr;
40+
}, '').trim() + omission;
41+
}
42+
43+
module.exports = truncate;

main.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
const globalOptions = require('./library/globalOptions')
2-
const fontDetect = require('./library/detector')
3-
const fontConvert = require('./library/converter')
4-
const syllBreak = require('./library/syllBreak')
5-
const spellingFix = require('./library/spellingCheck')
1+
const globalOptions = require('./library/globalOptions');
2+
const fontDetect = require('./library/detector');
3+
const fontConvert = require('./library/converter');
4+
const syllBreak = require('./library/syllBreak');
5+
const spellingFix = require('./library/spellingCheck');
6+
const truncate = require('./library/truncate');
67

7-
const {version} = require('./package.json')
8+
const {version} = require('./package.json');
89

910
module.exports = {
1011
version,
1112
setGlobalOptions: globalOptions.setOptions,
1213
fontDetect,
1314
fontConvert,
1415
syllBreak,
15-
spellingFix
16+
spellingFix,
17+
truncate,
1618
};

0 commit comments

Comments
 (0)