Compare commits
26 Commits
Author | SHA1 | Date | |
---|---|---|---|
1f31fe9dc5 | |||
bd7888f7ce | |||
201812e182 | |||
8dfa3f8965 | |||
43d50b746b | |||
a1ee8f37d6 | |||
8d9c75e113 | |||
8ff2a9d64a | |||
8665464416 | |||
e773644256 | |||
71fd0e614c | |||
55a5948a51 | |||
979375f08d | |||
95d5c6ceee | |||
c92ece79ff | |||
7c625c4390 | |||
65fc64f6aa | |||
8cda8c55c0 | |||
ec0b82de00 | |||
5e69664f59 | |||
f5838f6d6a | |||
fb4a03ad37 | |||
ceb6962a1c | |||
ffe757d820 | |||
60eb4b6b6e | |||
f73d394d35 |
22
.gitignore
vendored
22
.gitignore
vendored
@ -1,4 +1,22 @@
|
||||
.yarn/
|
||||
pages/
|
||||
.nogit/
|
||||
|
||||
# artifacts
|
||||
coverage/
|
||||
public/
|
||||
pages/
|
||||
|
||||
# installs
|
||||
node_modules/
|
||||
|
||||
# caches
|
||||
.yarn/
|
||||
.cache/
|
||||
.rpt2_cache
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_web/
|
||||
dist_serve/
|
||||
dist_ts_web/
|
||||
|
||||
# custom
|
132
.gitlab-ci.yml
132
.gitlab-ci.yml
@ -1,16 +1,16 @@
|
||||
# gitzone standard
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: "$CI_BUILD_STAGE"
|
||||
- .npmci_cache/
|
||||
key: '$CI_BUILD_STAGE'
|
||||
|
||||
stages:
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
@ -18,109 +18,103 @@ stages:
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
- npmci git mirror
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
snyk:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:snyk
|
||||
stage: security
|
||||
script:
|
||||
- npmci command npm install -g snyk
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
testLEGACY:
|
||||
stage: test
|
||||
script:
|
||||
- npmci node install legacy
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
allow_failure: true
|
||||
|
||||
testLTS:
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
testSTABLE:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# metadata stage
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
image: docker:stable
|
||||
allow_failure: true
|
||||
services:
|
||||
- docker:stable-dind
|
||||
script:
|
||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
||||
- docker run
|
||||
--env SOURCE_CODE="$PWD"
|
||||
--volume "$PWD":/code
|
||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
||||
artifacts:
|
||||
paths: [codeclimate.json]
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npm install -g typedoc typescript
|
||||
- npmci node install lts
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||
- npmci command tsdoc
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
@ -128,15 +122,5 @@ pages:
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
||||
|
||||
windowsCompatibility:
|
||||
image: stefanscherer/node-windows:10-build-tools
|
||||
stage: metadata
|
||||
script:
|
||||
- npm install & npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- windows
|
||||
- public
|
||||
allow_failure: true
|
||||
|
3
LICENSE
3
LICENSE
@ -1,6 +1,7 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Push.Rocks
|
||||
Copyright (c) 2014 Maurice Butler
|
||||
Copyright (c) 2016 Lossless GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
70
README.md
70
README.md
@ -1,70 +0,0 @@
|
||||
# smartstring
|
||||
|
||||
handle strings in smart ways. TypeScript ready.
|
||||
|
||||
## Availabililty
|
||||
|
||||
[](https://www.npmjs.com/package/smartstring)
|
||||
[](https://GitLab.com/pushrocks/smartstring)
|
||||
[](https://github.com/pushrocks/smartstring)
|
||||
[](https://pushrocks.gitlab.io/smartstring/)
|
||||
|
||||
## Status for master
|
||||
|
||||
[](https://GitLab.com/pushrocks/smartstring/commits/master)
|
||||
[](https://GitLab.com/pushrocks/smartstring/commits/master)
|
||||
[](https://www.npmjs.com/package/smartstring)
|
||||
[](https://david-dm.org/pushrocks/smartstring)
|
||||
[](https://www.bithound.io/github/pushrocks/smartstring/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/pushrocks/smartstring)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](http://standardjs.com/)
|
||||
|
||||
## Usage
|
||||
|
||||
Use TypeScript for best in class instellisense.
|
||||
|
||||
```javascript
|
||||
import * as smartstring from 'smartstring';
|
||||
|
||||
// classes
|
||||
|
||||
// smartstring.Domain class
|
||||
let myDomain = new smartstring.Domain('https://sub.main.tld');
|
||||
myDomain.level1; // "tld"
|
||||
myDomain.level2; // "main"
|
||||
// level3 , level 4 and so on...
|
||||
myDomain.zoneName; // "main.tld"
|
||||
myDOmain.protocol; // "https"
|
||||
|
||||
// smartstring.GitRepo class
|
||||
let myGitRepo = new smartstring.GitRepo('git@github.com:someorg/somerepo.git'); // takes https and git and npm repo URL versions
|
||||
myGitRepo.host; // "github.com"
|
||||
myGitRepo.user; // "someorg"
|
||||
myGitRepo.repo; // "somerepo"
|
||||
myGitRepo.accessToken; // accessToken if specified with https
|
||||
myGitRepo.sshUrl; // "git@github.com:someorg/somerepo.git" (computed also from https)
|
||||
myGitRepo.httpsUrl; // "https://github.com/someorg/somerepo.git" (computed also from ssh)
|
||||
|
||||
//smartstring.Base64
|
||||
let myBase64 = new smartstring.Base64('somestring', 'string'); // first arg is the string, second is string type (can be string, base64, base64uri)
|
||||
myBase64.simpleString; // 'somestring'
|
||||
myBase64.base64String; // base64 representation of 'somestring'
|
||||
myBase64.base64UriString; // base64uri representation of 'sometring'
|
||||
|
||||
// methods
|
||||
smartstring.base64.encode('somestring'); // encodes 'somestring' to base64
|
||||
smartstring.base64.encodeUri('sometring'); // encodes 'somestring' to base64uri
|
||||
smartstring.base64.decode(); // decodes base64 and base64uri to simple string respresentation
|
||||
|
||||
smartstring.indent.indent('somestringanotherstring', 4); // indents a string by 4
|
||||
smartstring.indent.indent('somestringanotherstring', '>>>> '); // indents a string with a prefix
|
||||
smartstring.indent.normalize(' somestring anotherstring', '>>>> '); // looks for the least amount of indention and removes superflouous space
|
||||
```
|
||||
|
||||
For further information read the linked docs at the top of this README.
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
|
||||
[](https://push.rocks)
|
@ -1,13 +1,21 @@
|
||||
{
|
||||
"npmci": {
|
||||
"npmGlobalTools": [
|
||||
"@gitzone/npmts"
|
||||
],
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"npmdocker": {
|
||||
"baseImage": "hosttoday/ht-docker-node:npmci",
|
||||
"command": "npmci node install stable && npmci npm install && npmci npm test",
|
||||
"dockerSock": false
|
||||
},
|
||||
"gitzone": {
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "pushrocks",
|
||||
"gitrepo": "smartstring",
|
||||
"shortDescription": "handle strings in smart ways. TypeScript ready.",
|
||||
"npmPackagename": "@pushrocks/smartstring",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
}
|
1614
package-lock.json
generated
1614
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
35
package.json
35
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartstring",
|
||||
"version": "3.0.3",
|
||||
"version": "3.0.16",
|
||||
"private": false,
|
||||
"description": "handle strings in smart ways. TypeScript ready.",
|
||||
"main": "dist/index.js",
|
||||
@ -24,17 +24,30 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartstring#readme",
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tsrun": "^1.1.12",
|
||||
"@gitzone/tstest": "^1.0.15",
|
||||
"@pushrocks/tapbundle": "^3.0.5",
|
||||
"@types/node": "^10.5.8"
|
||||
"@gitzone/tsbuild": "^2.1.17",
|
||||
"@gitzone/tsrun": "^1.2.8",
|
||||
"@gitzone/tstest": "^1.0.28",
|
||||
"@pushrocks/tapbundle": "^3.0.13",
|
||||
"@types/node": "^12.7.12",
|
||||
"tslint": "^5.20.0",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"crypto-random-string": "^1.0.0",
|
||||
"js-base64": "^2.4.8",
|
||||
"crypto-random-string": "^3.0.1",
|
||||
"js-base64": "^2.5.1",
|
||||
"normalize-newline": "^3.0.0",
|
||||
"randomatic": "^3.1.0",
|
||||
"strip-indent": "^2.0.0"
|
||||
}
|
||||
"randomatic": "^3.1.1",
|
||||
"strip-indent": "^3.0.0"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_web/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
]
|
||||
}
|
||||
|
19
test/test.normalize.ts
Normal file
19
test/test.normalize.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { tap, expect } from '@pushrocks/tapbundle';
|
||||
|
||||
import * as smartstring from '../ts/index';
|
||||
|
||||
tap.test('should normalize a string', async () => {
|
||||
const testString = `
|
||||
myawesome string;
|
||||
is indented with two spaces
|
||||
`;
|
||||
const normalizedString = smartstring.normalize.standard(testString);
|
||||
expect(normalizedString).to.equal(
|
||||
`
|
||||
myawesome string;
|
||||
is indented with two spaces
|
||||
`
|
||||
);
|
||||
});
|
||||
|
||||
tap.start();
|
14
test/test.type.ts
Normal file
14
test/test.type.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { tap, expect } from '@pushrocks/tapbundle';
|
||||
import * as smartstring from '../ts';
|
||||
|
||||
tap.test('should state valuid utf8', async () => {
|
||||
expect(smartstring.type.isUtf8('hithere')).to.be.true;
|
||||
});
|
||||
|
||||
tap.test('should state wether base64 string is valid', async () => {
|
||||
const base64String = smartstring.base64.encode('hi there');
|
||||
expect(smartstring.type.isBase64(base64String)).to.be.true;
|
||||
expect(smartstring.type.isBase64('hi there')).to.be.false;
|
||||
});
|
||||
|
||||
tap.start();
|
@ -2,9 +2,12 @@ import * as create from './smartstring.create';
|
||||
import * as docker from './smartstring.docker';
|
||||
import * as indent from './smartstring.indent';
|
||||
import * as normalize from './smartstring.normalize';
|
||||
import * as type from './smartstring.type';
|
||||
|
||||
export { create, docker, normalize, indent };
|
||||
export { create, docker, normalize, indent, type };
|
||||
|
||||
export { Base64, base64 } from './smartstring.base64';
|
||||
export { Domain } from './smartstring.domain';
|
||||
export { GitRepo } from './smartstring.git';
|
||||
|
||||
export { Cryptr } from './smartstring.encryption';
|
||||
|
@ -1,13 +1,29 @@
|
||||
import * as plugins from './smartstring.plugins';
|
||||
|
||||
/**
|
||||
* creates a random string
|
||||
*
|
||||
* ```ts
|
||||
* createRandomString('AAAA')
|
||||
* //=> 'AGHR'
|
||||
* ```
|
||||
*
|
||||
* @param patternArg the pattern argument to use, Aa0!* are viable pattern descritors
|
||||
* @param lengthArg the length of the random string
|
||||
* @param optionsArg options
|
||||
*/
|
||||
export const createRandomString = (
|
||||
patternArg: string,
|
||||
lengthArg: number,
|
||||
optionsArg: any
|
||||
lengthArg?: number,
|
||||
optionsArg?: any
|
||||
): string => {
|
||||
return plugins.randomatic(patternArg, lengthArg, optionsArg);
|
||||
};
|
||||
|
||||
export const createCryptoRandomString = (lengthArg): string => {
|
||||
/**
|
||||
* creates a crytic string in the speicifed length
|
||||
* @param lengthArg the length of the crypto string
|
||||
*/
|
||||
export const createCryptoRandomString = (lengthArg: number): string => {
|
||||
return plugins.cryptoRandomString(lengthArg);
|
||||
};
|
||||
|
@ -1,24 +1,26 @@
|
||||
import * as plugins from './smartstring.plugins';
|
||||
|
||||
export class Domain {
|
||||
fullName: string;
|
||||
level1: string;
|
||||
level2: string;
|
||||
level3: string;
|
||||
level4: string;
|
||||
level5: string;
|
||||
protocol: string;
|
||||
zoneName: string;
|
||||
public fullName: string;
|
||||
public level1: string;
|
||||
public level2: string;
|
||||
public level3: string;
|
||||
public level4: string;
|
||||
public level5: string;
|
||||
public protocol: string;
|
||||
public zoneName: string;
|
||||
// aliases
|
||||
topLevel: string;
|
||||
domainName;
|
||||
subDomain;
|
||||
public topLevel: string;
|
||||
public domainName;
|
||||
public subDomain;
|
||||
public port;
|
||||
public nodeParsedUrl: plugins.url.UrlWithStringQuery;
|
||||
constructor(domainStringArg: string) {
|
||||
let regexMatches = domainRegex(domainStringArg);
|
||||
const regexMatches = this._domainRegex(domainStringArg);
|
||||
this.fullName = '';
|
||||
for (let i = 1; i <= 5; i++) {
|
||||
if (regexMatches[i - 1]) {
|
||||
let localMatch = regexMatches[i - 1];
|
||||
const localMatch = regexMatches[i - 1];
|
||||
this['level' + i.toString()] = localMatch;
|
||||
if (this.fullName === '') {
|
||||
this.fullName = localMatch;
|
||||
@ -29,33 +31,49 @@ export class Domain {
|
||||
this['level' + i.toString()] = undefined;
|
||||
}
|
||||
}
|
||||
this.protocol = protocolRegex(domainStringArg);
|
||||
this.protocol = this._protocolRegex(domainStringArg);
|
||||
this.zoneName = this.level2 + '.' + this.level1;
|
||||
|
||||
// aliases
|
||||
this.topLevel = this.level1;
|
||||
this.domainName = this.level2;
|
||||
this.subDomain = this.level3;
|
||||
|
||||
this.nodeParsedUrl = plugins.url.parse(domainStringArg);
|
||||
this.port = this.nodeParsedUrl.port;
|
||||
}
|
||||
|
||||
// helper functions
|
||||
|
||||
/** */
|
||||
private _domainRegex(stringArg: string) {
|
||||
const regexString = /([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}$/;
|
||||
const regexMatches = regexString.exec(stringArg);
|
||||
regexMatches.reverse(); //make sure we build the domain from toplevel to subdomain (reversed order)
|
||||
regexMatches.pop(); // pop the last element, which is, since we reversed the Array, the full String of matched elements
|
||||
const regexMatchesFiltered = regexMatches.filter(function(stringArg: string) {
|
||||
return stringArg !== '';
|
||||
});
|
||||
return regexMatchesFiltered;
|
||||
}
|
||||
|
||||
private _protocolRegex(stringArg: string) {
|
||||
const regexString = /^([a-zA-Z0-9]*):\/\//;
|
||||
const regexMatches = regexString.exec(stringArg);
|
||||
if (regexMatches) {
|
||||
return regexMatches[1];
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
private _portRegex(stringArg: string) {
|
||||
const regexString = /^([a-zA-Z0-9]*):\/\//;
|
||||
const regexMatches = regexString.exec(stringArg);
|
||||
if (regexMatches) {
|
||||
return regexMatches[1];
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let domainRegex = function(stringArg: string) {
|
||||
let regexString = /([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}$/;
|
||||
let regexMatches = regexString.exec(stringArg);
|
||||
regexMatches.reverse(); //make sure we build the domain from toplevel to subdomain (reversed order)
|
||||
regexMatches.pop(); // pop the last element, which is, since we reversed the Array, the full String of matched elements
|
||||
let regexMatchesFiltered = regexMatches.filter(function(stringArg: string) {
|
||||
return stringArg !== '';
|
||||
});
|
||||
return regexMatchesFiltered;
|
||||
};
|
||||
|
||||
let protocolRegex = function(stringArg: string) {
|
||||
let regexString = /^([a-zA-Z0-9]*):\/\//;
|
||||
let regexMatches = regexString.exec(stringArg);
|
||||
if (regexMatches) {
|
||||
return regexMatches[1];
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
43
ts/smartstring.encryption.ts
Normal file
43
ts/smartstring.encryption.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import * as plugins from './smartstring.plugins';
|
||||
|
||||
const algorithm = 'aes-256-ctr';
|
||||
|
||||
export class Cryptr {
|
||||
private key: Buffer;
|
||||
|
||||
constructor(secret) {
|
||||
if (!secret || typeof secret !== 'string') {
|
||||
throw new Error('Cryptr: secret must be a non-0-length string');
|
||||
}
|
||||
|
||||
this.key = plugins.crypto
|
||||
.createHash('sha256')
|
||||
.update(String(secret))
|
||||
.digest();
|
||||
}
|
||||
|
||||
encrypt(value: string) {
|
||||
if (value == null) {
|
||||
throw new Error('value must not be null or undefined');
|
||||
}
|
||||
|
||||
const iv = plugins.crypto.randomBytes(16);
|
||||
const cipher = plugins.crypto.createCipheriv(algorithm, this.key, iv);
|
||||
const encrypted = cipher.update(String(value), 'utf8', 'hex') + cipher.final('hex');
|
||||
|
||||
return iv.toString('hex') + encrypted;
|
||||
}
|
||||
|
||||
decrypt(value: string) {
|
||||
if (value == null) {
|
||||
throw new Error('value must not be null or undefined');
|
||||
}
|
||||
|
||||
const stringValue = String(value);
|
||||
const iv = Buffer.from(stringValue.slice(0, 32), 'hex');
|
||||
const encrypted = stringValue.slice(32);
|
||||
|
||||
const decipher = plugins.crypto.createDecipheriv(algorithm, this.key, iv);
|
||||
return decipher.update(encrypted, 'hex', 'utf8') + decipher.final('utf8');
|
||||
}
|
||||
}
|
@ -3,11 +3,11 @@ import * as plugins from './smartstring.plugins';
|
||||
/**
|
||||
* replaces all occurences of something in a string
|
||||
* @param stringArg
|
||||
* @param searchRegExp
|
||||
* @param searchPattern
|
||||
* @param replacementString
|
||||
*/
|
||||
export const replaceAll = (stringArg: string, searchRegExp: any, replacementString: string) => {
|
||||
return stringArg.replace(new RegExp(searchRegExp, 'g'), replacementString);
|
||||
export const replaceAll = (stringArg: string, searchPattern: string, replacementString: string) => {
|
||||
return stringArg.replace(new RegExp(searchPattern, 'g'), replacementString);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -17,6 +17,6 @@ export const replaceAll = (stringArg: string, searchRegExp: any, replacementStri
|
||||
export const standard = (stringArg: string): string => {
|
||||
let fix1 = plugins.stripIndent(stringArg); // fix indention
|
||||
let fix2 = plugins.normalizeNewline(fix1); // fix newlines
|
||||
let fix3 = replaceAll(fix2, /\t/, ' '); // fix tabs
|
||||
let fix3 = replaceAll(fix2, '\t/', ' '); // fix tabs
|
||||
return fix3;
|
||||
};
|
||||
|
@ -1,3 +1,10 @@
|
||||
// node native
|
||||
import crypto from 'crypto';
|
||||
import * as url from 'url';
|
||||
|
||||
export { crypto, url };
|
||||
|
||||
// third party
|
||||
export let jsBase64 = require('js-base64').Base64;
|
||||
|
||||
export let stripIndent = require('strip-indent');
|
||||
|
99
ts/smartstring.type.ts
Normal file
99
ts/smartstring.type.ts
Normal file
@ -0,0 +1,99 @@
|
||||
import * as plugins from './smartstring.plugins';
|
||||
import * as base64 from './smartstring.base64';
|
||||
|
||||
export const isUtf8 = (stringArg: string) => {
|
||||
const bytes = Buffer.from(stringArg);
|
||||
let i = 0;
|
||||
while (i < bytes.length) {
|
||||
if (
|
||||
// ASCII
|
||||
bytes[i] === 0x09 ||
|
||||
bytes[i] === 0x0a ||
|
||||
bytes[i] === 0x0d ||
|
||||
(0x20 <= bytes[i] && bytes[i] <= 0x7e)
|
||||
) {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
// non-overlong 2-byte
|
||||
0xc2 <= bytes[i] &&
|
||||
bytes[i] <= 0xdf &&
|
||||
0x80 <= bytes[i + 1] &&
|
||||
bytes[i + 1] <= 0xbf
|
||||
) {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
// excluding overlongs
|
||||
(bytes[i] === 0xe0 &&
|
||||
0xa0 <= bytes[i + 1] &&
|
||||
bytes[i + 1] <= 0xbf &&
|
||||
0x80 <= bytes[i + 2] &&
|
||||
bytes[i + 2] <= 0xbf) || // straight 3-byte
|
||||
(((0xe1 <= bytes[i] && bytes[i] <= 0xec) || bytes[i] === 0xee || bytes[i] === 0xef) &&
|
||||
0x80 <= bytes[i + 1] &&
|
||||
bytes[i + 1] <= 0xbf &&
|
||||
0x80 <= bytes[i + 2] &&
|
||||
bytes[i + 2] <= 0xbf) || // excluding surrogates
|
||||
(bytes[i] === 0xed &&
|
||||
0x80 <= bytes[i + 1] &&
|
||||
bytes[i + 1] <= 0x9f &&
|
||||
0x80 <= bytes[i + 2] &&
|
||||
bytes[i + 2] <= 0xbf)
|
||||
) {
|
||||
i += 3;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
// planes 1-3
|
||||
(bytes[i] === 0xf0 &&
|
||||
0x90 <= bytes[i + 1] &&
|
||||
bytes[i + 1] <= 0xbf &&
|
||||
0x80 <= bytes[i + 2] &&
|
||||
bytes[i + 2] <= 0xbf &&
|
||||
0x80 <= bytes[i + 3] &&
|
||||
bytes[i + 3] <= 0xbf) || // planes 4-15
|
||||
(0xf1 <= bytes[i] &&
|
||||
bytes[i] <= 0xf3 &&
|
||||
0x80 <= bytes[i + 1] &&
|
||||
bytes[i + 1] <= 0xbf &&
|
||||
0x80 <= bytes[i + 2] &&
|
||||
bytes[i + 2] <= 0xbf &&
|
||||
0x80 <= bytes[i + 3] &&
|
||||
bytes[i + 3] <= 0xbf) || // plane 16
|
||||
(bytes[i] === 0xf4 &&
|
||||
0x80 <= bytes[i + 1] &&
|
||||
bytes[i + 1] <= 0x8f &&
|
||||
0x80 <= bytes[i + 2] &&
|
||||
bytes[i + 2] <= 0xbf &&
|
||||
0x80 <= bytes[i + 3] &&
|
||||
bytes[i + 3] <= 0xbf)
|
||||
) {
|
||||
i += 4;
|
||||
continue;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
export const isBase64 = (stringArg: string) => {
|
||||
const notBase64 = /[^A-Z0-9+\/=]/i;
|
||||
const len = stringArg.length;
|
||||
if (!len || len % 4 !== 0 || notBase64.test(stringArg)) {
|
||||
return false;
|
||||
}
|
||||
const firstPaddingChar = stringArg.indexOf('=');
|
||||
return (
|
||||
firstPaddingChar === -1 ||
|
||||
firstPaddingChar === len - 1 ||
|
||||
(firstPaddingChar === len - 2 && stringArg[len - 1] === '=')
|
||||
);
|
||||
};
|
16
tslint.json
16
tslint.json
@ -1,3 +1,17 @@
|
||||
{
|
||||
"extends": "tslint-config-standard"
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
||||
|
Reference in New Issue
Block a user