Skip to content

Commit

Permalink
Merge pull request #6 from inkOfPixel/new-version
Browse files Browse the repository at this point in the history
Version 0.2.0

Simplify the lib code and introduce some CI updates to run tests on newer node versions
  • Loading branch information
federicobadini authored Jan 22, 2024
2 parents 370c179 + b3f1491 commit 366562e
Show file tree
Hide file tree
Showing 6 changed files with 1,920 additions and 3,955 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node: ['10.x', '12.x', '14.x']
node: ["10.x", "12.x", "14.x", "16.x", "18.x"]
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
Expand Down
12 changes: 0 additions & 12 deletions .github/workflows/size.yml

This file was deleted.

32 changes: 9 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sms-length",
"author": "inkOfPixel srl",
"version": "0.1.2",
"version": "0.2.0",
"license": "MIT",
"description": "A simple utility function to understand how many sms will be required to send a text message via SMS. Largely inspired by sms-counter.",
"keywords": [
Expand Down Expand Up @@ -29,41 +29,27 @@
"node": ">=10"
},
"sideEffects": false,
"devDependencies": {
"@size-limit/preset-small-lib": "4.11.0",
"eslint-plugin-prettier": "^3.4.0",
"husky": "6.0.0",
"prettier": "2.3.0",
"size-limit": "4.11.0",
"tsdx": "0.14.1",
"tslib": "2.2.0",
"typescript": "4.2.4"
},
"scripts": {
"start": "tsdx watch",
"build": "tsdx build",
"test": "tsdx test",
"lint": "tsdx lint",
"prepare": "tsdx build",
"size": "size-limit",
"analyze": "size-limit --why",
"format": "prettier --write './**/*.ts'"
},
"devDependencies": {
"eslint-plugin-prettier": "3.4.0",
"husky": "6.0.0",
"prettier": "2.3.0",
"tsdx": "0.14.1",
"tslib": "2.2.0",
"typescript": "4.2.4"
},
"husky": {
"hooks": {
"pre-commit": "yarn lint && yarn test"
}
},
"size-limit": [
{
"path": "dist/sms-length.cjs.production.min.js",
"limit": "10 KB"
},
{
"path": "dist/sms-length.esm.js",
"limit": "10 KB"
}
],
"prettier": {
"endOfLine": "auto"
}
Expand Down
39 changes: 13 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const GSM_7BIT_REGEXP =
/^[@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&'()*+,\-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà]*$/;
const GSM_7BIT_EXT_REGEXP =
/^[@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&'()*+,\-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^{}\\[~\]|€]*$/;
const GSM_7BIT_EXT_CHAR_REGEXP = /^[\^{}\\[~\]|€]$/;
const GSM_7BIT_EXT_CHAR_REGEXP = /[\^{}\\[~\]|€]/g;

const messageLength: { [key in Encoding]: number } = {
GSM_7BIT: 160,
Expand All @@ -20,11 +20,10 @@ const multiMessageLength: { [key in Encoding]: number } = {

export const count = (text: string) => {
const encoding = detectEncoding(text);
let length = text.length;

if (encoding === "GSM_7BIT_EXT") {
length += countGsm7bitExt(text);
}
const length =
encoding === "GSM_7BIT_EXT"
? text.length + (text.match(GSM_7BIT_EXT_CHAR_REGEXP) ?? []).length
: text.length;

let characterPerMessage = messageLength[encoding];
if (length > characterPerMessage) {
Expand All @@ -33,10 +32,8 @@ export const count = (text: string) => {

const messages = Math.ceil(length / characterPerMessage);

let inCurrentMessage = length;
if (messages > 0) {
inCurrentMessage = length - characterPerMessage * (messages - 1);
}
const inCurrentMessage =
messages > 0 ? length - characterPerMessage * (messages - 1) : length;

let remaining = characterPerMessage * messages - length;
if (remaining === 0 && messages === 0) {
Expand All @@ -54,23 +51,13 @@ export const count = (text: string) => {
};

const detectEncoding = (text: string): Encoding => {
switch (false) {
case text.match(GSM_7BIT_REGEXP) == null:
return "GSM_7BIT";
case text.match(GSM_7BIT_EXT_REGEXP) == null:
return "GSM_7BIT_EXT";
default:
return "UTF16";
if (text.match(GSM_7BIT_REGEXP) != null) {
return "GSM_7BIT";
}
};

const countGsm7bitExt = (text: string) => {
const extChar = [];
for (let i = 0; i < text.length; i++) {
const char = text[i];
if ((char.match(GSM_7BIT_EXT_CHAR_REGEXP) || []).length > 0) {
extChar.push(char);
}
if (text.match(GSM_7BIT_EXT_REGEXP) != null) {
return "GSM_7BIT_EXT";
}
return extChar.length;

return "UTF16";
};
10 changes: 5 additions & 5 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { count } from "../src/index";

test("Single message with only default characters", async () => {
const message = "simple message";

const response = count(message);

expect(response).toEqual({
encoding: "GSM_7BIT",
length: 14,
Expand All @@ -17,8 +17,8 @@ test("Single message with only default characters", async () => {
test("Test all GSM_7BIT chars", async () => {
const message =
" !\"#$%&'()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz\n\rABCDEFGHIJKLMNOPQRSTUVWXYZ£¥§¿_ΔΦΓΛΩΠΨΣΘΞèéùìòÇØøÅåÆæßÉÄÖÑÜäöñüड";

const response = count(message);

expect(response).toEqual({
encoding: "GSM_7BIT",
length: 127,
Expand All @@ -31,8 +31,8 @@ test("Test all GSM_7BIT chars", async () => {

test("Test all GSM_7BIT_EXT chars", async () => {
const message = "^{}\\[~]|€";

const response = count(message);

expect(response).toEqual({
encoding: "GSM_7BIT_EXT",
length: 18,
Expand All @@ -45,8 +45,8 @@ test("Test all GSM_7BIT_EXT chars", async () => {

test("Special chars are treated as unicode - no shift table are enabled", async () => {
const message = "Â";

const response = count(message);

expect(response).toEqual({
encoding: "UTF16",
length: 1,
Expand All @@ -65,8 +65,8 @@ Consegna Gratuita.
Ordinali Subito su https://ysms.me/u/3IAeLge
Per Rinunciare https://ysms.me/u/wNT2Bb3`;

const response = count(message);

expect(response).toEqual({
encoding: "GSM_7BIT_EXT",
length: 307,
Expand Down
Loading

0 comments on commit 366562e

Please sign in to comment.