Commit 5865e7f0 authored by rockyl's avatar rockyl

init

parents
### Node template
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
# dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
/debug
/types
/src
*.iml
\ No newline at end of file
/**
* Created by rockyl on 2021/10/19.
*/
/**
* 类啊
* 说明呢
*/
class Base{
//名称
name
//年龄
age
}
/**
* Created by rockyl on 2021/10/19.
*/
export interface Options {
/**包含注释,默认值:false*/
includeComments?: boolean;
/**包含空行,默认值:false*/
includeWhiteLines?: boolean;
}
/**
* 获取代码行数
* @param code - 代码
* @param options - 配置参数
*/
export declare function getLinesOfCode(code: string, options?: Options): number;
"use strict";
/**
* Created by rockyl on 2021/10/19.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLinesOfCode = void 0;
const commentStarts = ['//', '/**', '* ', '*/'];
/**
* 获取代码行数
* @param code - 代码
* @param options - 配置参数
*/
function getLinesOfCode(code, options = {}) {
const { includeComments = false, includeWhiteLines = false } = options;
const lines = code.split('\n');
let lineNum = 0;
for (let line of lines) {
const trimLine = line.trimLeft();
if (!includeWhiteLines && trimLine.length === 0) {
continue;
}
if (!includeComments && commentStarts.filter(starts => trimLine.startsWith(starts)).length > 0) {
continue;
}
lineNum++;
}
return lineNum;
}
exports.getLinesOfCode = getLinesOfCode;
//# sourceMappingURL=index.js.map
\ No newline at end of file
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,MAAM,aAAa,GAAG,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,CAAA;AAS/C;;;;GAIG;AACH,SAAgB,cAAc,CAAC,IAAY,EAAE,UAAmB,EAAE;IACjE,MAAM,EAAC,eAAe,GAAG,KAAK,EAAE,iBAAiB,GAAG,KAAK,EAAC,GAAG,OAAO,CAAA;IAEpE,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IAC9B,IAAI,OAAO,GAAG,CAAC,CAAA;IACf,KAAK,IAAI,IAAI,IAAI,KAAK,EAAE;QACvB,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAA;QAChC,IAAI,CAAC,iBAAiB,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE;YAChD,SAAQ;SACR;QACD,IAAI,CAAC,eAAe,IAAI,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,QAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE;YAC/F,SAAQ;SACR;QACD,OAAO,EAAE,CAAA;KACT;IACD,OAAO,OAAO,CAAA;AACf,CAAC;AAhBD,wCAgBC"}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
{
"name": "loc",
"version": "1.0.2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
"scripts": {
"dev": "tsc -w",
"build": "tsc",
"release": "npm run build && pvi && npm publish"
},
"dependencies": {
"tslib": "^2.2.0"
},
"devDependencies": {
"package-version-increment": "^1.0.2",
"typescript": "^4.2.4"
}
}
\ No newline at end of file
# lines-of-code
计算代码行号
_默认排除注释和空行_
## Install
`yarn add loc`
or
`npm i loc`
## Usage
```js
import {getLinesOfCode} from "loc"
const lines = getLinesOfCode('some codes...')
```
### Api
获取代码行数
`getLinesOfCode: (code: string, options?: Options)=>number`
### Options
| 名称 | 类型 | 必须 | 默认值 | 说明 |
| :---- | :---- | :---- | :---- | :---- |
| `includeComments` | `boolean` | 否 | `false` | 包含注释 |
| `includeWhiteLines` | `boolean` | 否 | `false` | 包含空行 |
/**
* Created by rockyl on 2021/10/19.
*/
const commentStarts = ['//', '/**', '* ', '*/']
export interface Options {
/**包含注释,默认值:false*/
includeComments?: boolean
/**包含空行,默认值:false*/
includeWhiteLines?: boolean
}
/**
* 获取代码行数
* @param code - 代码
* @param options - 配置参数
*/
export function getLinesOfCode(code: string, options: Options = {}): number {
const {includeComments = false, includeWhiteLines = false} = options
const lines = code.split('\n')
let lineNum = 0
for (let line of lines) {
const trimLine = line.trimLeft()
if (!includeWhiteLines && trimLine.length === 0) {
continue
}
if (!includeComments && commentStarts.filter(starts => trimLine.startsWith(starts)).length > 0) {
continue
}
lineNum++
}
return lineNum
}
/**
* Created by rockyl on 2021/10/19.
*/
const fs = require('fs')
const {getLinesOfCode} = require("./dist")
fs.readFile('code.js', 'utf-8', function(err, content){
console.log(getLinesOfCode(content))
})
{
"compilerOptions": {
"module": "commonjs",
"target": "ESNext",
"sourceMap": true,
"outDir": "dist",
"declaration": true,
"moduleResolution": "Node",
"esModuleInterop": true,
"importHelpers": true,
"resolveJsonModule": true,
"lib": [
"ESNext"
]
},
"include": [
"src"
],
"exclude": [
"node_modules"
]
}
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
lru-cache@^6.0.0:
version "6.0.0"
resolved "http://npm.dui88.com:80/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
integrity sha1-bW/mVw69lqr5D8rR2vo7JWbbOpQ=
dependencies:
yallist "^4.0.0"
package-version-increment@^1.0.2:
version "1.0.2"
resolved "http://npm.dui88.com:80/package-version-increment/-/package-version-increment-1.0.2.tgz#5940f1acfa1eecfcf2e4fea7cd4fb9a678df74bf"
integrity sha1-WUDxrPoe7Pzy5P6nzU+5pnjfdL8=
dependencies:
semver "^7.3.5"
semver@^7.3.5:
version "7.3.5"
resolved "http://npm.dui88.com:80/semver/-/semver-7.3.5.tgz#0b621c879348d8998e4b0e4be94b3f12e6018ef7"
integrity sha1-C2Ich5NI2JmOSw5L6Us/EuYBjvc=
dependencies:
lru-cache "^6.0.0"
tslib@^2.2.0:
version "2.3.1"
resolved "http://npm.dui88.com:80/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01"
integrity sha1-6KM1rdXOrlGqJh0ypJAVjvBC7wE=
typescript@^4.2.4:
version "4.4.4"
resolved "http://npm.dui88.com:80/typescript/-/typescript-4.4.4.tgz#2cd01a1a1f160704d3101fd5a58ff0f9fcb8030c"
integrity sha1-LNAaGh8WBwTTEB/VpY/w+fy4Aww=
yallist@^4.0.0:
version "4.0.0"
resolved "http://npm.dui88.com:80/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
integrity sha1-m7knkNnA7/7GO+c1GeEaNQGaOnI=
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment