Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 16, 2021
0 parents commit 2189614
Show file tree
Hide file tree
Showing 15 changed files with 6,462 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

[*.js]
indent_style = space
indent_size = 2

[{package.json,*.yml,*.cjson}]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"@nuxtjs/eslint-config-typescript"
]
}
Empty file added .github/banner.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: ci

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
ci:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest]
node: [14]

steps:
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}

- name: checkout
uses: actions/checkout@master

- name: cache node_modules
uses: actions/cache@v1
with:
path: node_modules
key: ${{ matrix.os }}-node-v${{ matrix.node }}-deps-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }}

- name: Install dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: yarn

- name: Lint
run: yarn lint

- name: Build
run: yarn build

- name: Test
run: yarn jest

- name: Coverage
uses: codecov/codecov-action@v1
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.vscode
node_modules
*.log
.DS_Store
coverage
dist
types
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Nuxt Contrib

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
92 changes: 92 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# 🧵 Scule

[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![Github Actions][github-actions-src]][github-actions-href]
[![Codecov][codecov-src]][codecov-href]
[![bundle][bundle-src]][bundle-href]

<!-- ![](.github/banner.svg) -->

## Install

Install using npm or yarn:

```bash
npm i scule
# or
yarn add scule
```

Import:

```js
// CommonJS
const { pascalCase } = require('scule')

// ESM
import { pascalCase } from 'scule'
```

**Notice:** You may need to transpile package for legacy environments

## Utils

### `pascalCase(str)`

Splits string and joins by PascalCase convention (`foo-bar` => `FooBar`)

**Remarks:**

- If an upper case if followed by other upper case chars (like `FooBAR`), it is preserved

### `camelCase`

Splits string and joins by camelCase convention (`foo-bar` => `fooBar`)

### `kebabCase(str)`

Splits string and joins by kebab-case convention (`fooBar` => `foo-bar`)

**Remarks:**

- It does **not** preserves case

### `snakeCase`

Splits string and joins by snake_case convention (`foo-bar` => `foo_bar`)

### `upperFirst(str)`

Converts first charachter to upper case

### `lowerFirst(str)`

Converts first charachter to lower case

### `splitByCase(str, splitters?)`

- Splits string by splitters (default: `['-', '_', '/']`)
- Splits when case changes from lower to upper (only rising edges)
- Case is preserved in returned value
- Is an irreversible function since splitters are omitted

## License

[MIT](./LICENSE)

<!-- Badges -->
[npm-version-src]: https://img.shields.io/npm/v/scule?style=flat-square
[npm-version-href]: https://npmjs.com/package/scule

[npm-downloads-src]: https://img.shields.io/npm/dm/scule?style=flat-square
[npm-downloads-href]: https://npmjs.com/package/scule

[github-actions-src]: https://img.shields.io/github/workflow/status/nuxt-contrib/scule/ci/main?style=flat-square
[github-actions-href]: https://github.com/nuxt-contrib/scule/actions?query=workflow%3Aci

[codecov-src]: https://img.shields.io/codecov/c/gh/nuxt-contrib/scule/main?style=flat-square
[codecov-href]: https://codecov.io/gh/nuxt-contrib/scule

[bundle-src]: https://img.shields.io/bundlephobia/minzip/scule?style=flat-square
[bundle-href]: https://bundlephobia.com/result?p=scule
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
preset: 'ts-jest',
collectCoverage: true,
testEnvironment: 'node'
}
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "scule",
"version": "0.0.0",
"description": "",
"repository": "nuxt-contrib/scule",
"license": "MIT",
"sideEffects": false,
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "siroc build",
"lint": "eslint --ext .ts .",
"prepublishOnly": "yarn build",
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
"test": "yarn lint && jest"
},
"dependencies": {},
"devDependencies": {
"@nuxtjs/eslint-config-typescript": "latest",
"@types/flat": "latest",
"@types/jest": "latest",
"@types/node": "latest",
"eslint": "latest",
"jest": "latest",
"siroc": "latest",
"standard-version": "latest",
"ts-jest": "latest",
"typescript": "latest"
}
}
5 changes: 5 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"@nuxtjs"
]
}
73 changes: 73 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
export function isUppercase (char: string = '') {
return char.toUpperCase() === char
}

const STR_SPLITTERS = ['-', '_', '/']

export function splitByCase (str: string, splitters = STR_SPLITTERS): string[] {
const parts: string[] = []

let buff = ''

let previusUpper = isUppercase(str[0])
let previousSplitter = splitters.includes(str[0])

for (const char of str.split('')) {
const isSplitter = splitters.includes(char)
if (isSplitter) {
parts.push(buff)
buff = ''
previusUpper = false
previousSplitter = true
} else if (!previousSplitter && !previusUpper && isUppercase(char)) {
parts.push(buff)
buff = char
previusUpper = true
previousSplitter = false
} else {
buff += char
previusUpper = isUppercase(char)
previousSplitter = isSplitter
}
}

if (buff) {
parts.push(buff)
}

return parts
}

export function upperFirst (str: string): string {
if (!str) {
return ''
}
return str[0].toUpperCase() + str.substr(1)
}

export function lowerFirst (str: string): string {
if (!str) {
return ''
}
return str[0].toLocaleLowerCase() + str.substr(1)
}

export function pascalCase (str: string | string[] = ''): string {
return (Array.isArray(str) ? str : splitByCase(str))
.map(p => upperFirst(p))
.join('')
}

export function camelCase (str: string | string[] = ''): string {
return lowerFirst(pascalCase(str))
}

export function kebabCase (str: string | string[] = '', joiner = '-'): string {
return (Array.isArray(str) ? str : splitByCase(str))
.map((p = '') => p.toLocaleLowerCase())
.join(joiner)
}

export function snakeCase (str: string | string[] = '') {
return kebabCase(str, '_')
}
Loading

0 comments on commit 2189614

Please sign in to comment.