Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x, 16.x, 18.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm test
87 changes: 81 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,91 @@
exports = module.exports = trim;

function trim(str){
if (str.trim) return str.trim();
return str.replace(/^\s*|\s*$/g, '');
if (typeof str.trim === "function") {
return str.trim();
}

return trimLeft(trimRight(str));
}

exports.left = function(str){
if (str.trimLeft) return str.trimLeft();
return str.replace(/^\s*/, '');
if (typeof str.trimStart === "function") {
return str.trimStart();
}

return typeof str.trimLeft === "function" ? str.trimLeft() : trimLeft(str);
};

exports.right = function(str){
if (str.trimRight) return str.trimRight();
return str.replace(/\s*$/, '');
if (typeof str.trimEnd === "function") {
return str.trimEnd();
}

return typeof str.trimRight === "function" ? str.trimRight() : trimRight(str);
};

var getFirstNonSpaceIndex = function(str, isRightTrim) {
if (isEmpty(str)) {
return -1;
}

if (isRightTrim) {
var nonSpaceIndex = str.length - 1;
while (nonSpaceIndex > -1) {
if (isSpaceChar(str[nonSpaceIndex])) {
nonSpaceIndex--;
continue;
}

return nonSpaceIndex;
}
}

var nonSpaceIndex = 0;
var maxLength = str.length;
while (nonSpaceIndex < maxLength) {
if (isSpaceChar(str[nonSpaceIndex])) {
nonSpaceIndex++;
continue;
}

break;
}

return nonSpaceIndex >= maxLength ? -1 : nonSpaceIndex;
}

var isEmpty = function (str) {
return typeof str !== "string" || str.length === 0;
}

var isSpaceChar = function (char) {
return [" ", "\n", "\r"].indexOf(char) !== -1;
}

var trimLeft = function(str) {
if (isEmpty(str) || !isSpaceChar(str[0])) {
return str;
}

var nonSpaceIndex = getFirstNonSpaceIndex(str, false);
if (nonSpaceIndex < 0) {
return "";
}

return str.slice(nonSpaceIndex, str.length);
}

var trimRight = function(str) {
if (isEmpty(str) || !isSpaceChar(str[str.length - 1])) {
return str;
}

var nonSpaceIndex = getFirstNonSpaceIndex(str, true);
if (nonSpaceIndex < 0) {
return "";
}

var firstSpaceIndexFromTheEnd = nonSpaceIndex + 1;
return str.slice(0, firstSpaceIndexFromTheEnd);
}
Loading