npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mornya/primitive-type-libs

v0.6.2

Published

The project of JavaScript Primitive Type extension library.

Downloads

15

Readme

Primitive Type Libs

npm node types downloads license

Prototype methods of JavaScript Primitive Types.

This project has been created by Vessel. For a simple and quick reference, click here.

About

자바스크립트 Primitive Type에 대한 확장 프로토타입 구현 라이브러리.

Installation

해당 라이브러리를 사용 할 프로젝트에서는 아래와 같이 의존성 모듈로 설치한다.

$ npm install --save @mornya/primitive-type-libs
or
$ yarn add @mornya/primitive-type-libs

Usage

아래와 같이 사용 할 모듈을 import 하여 사용한다.

import { PrimitiveType } from '@mornya/primitive-type-libs';
import { move } from '@mornya/primitive-type-libs/dist/array';
import { format } from '@mornya/primitive-type-libs/dist/date';

const option: PrimitiveType.Option = {
  array: [move],
  date: [format],
};

PrimitiveType.initialize(option);

console.log(Array.move);
console.log(Date.format);

타입 정의를 위해 tsconfig.json 파일에 아래와 같이 include 항목에 경로를 추가하고 exclude 항목에는 node_modules 경로를 제외하는 설정을 해주어야 한다.

{
  ...

  "include": {
    "node_modules/@mornya/primitive-type-libs/types/**/*"
  },
  "exclude": {
    // "node_modules"
  }
}

Modules in the package

본 패키지에는 아래와 같은 모듈들을 포함한다. 제공되는 모듈과 메소드 사용법 등은 코드 스니핏을 참고한다.

Array module

Array.move

지정한 숫자만큼 배열 아이템의 순서를 이동한다.

let value = [1, 2, 3];
value = value.move(1, 0);
console.log(value); // [2, 1, 3]

Array.removeMatch

지정한 배열과 중복되는 배열 내 아이템을 제거한다.

let value = [1, 2, 3, 4, 5];
value = value.removeMatch([3, 5]);
console.log(value); // [1, 2, 4]

Array.shuffle

배열 내 아이템을 랜덤하게 섞는다.

let value = [1, 2, 3, 4, 5];
value = value.shuffle();
console.log(value); // [5, 1, 3, 2, 4]...

Array.sortObject

Object Type의 리스트를 소팅한다.

let value = [
  { id: '1', value: 'def' },
  { id: '0', value: 'abc' },
];
value = value.sortObject('value');
console.log(value); // [{ id: '0', value: 'abc' },{ id: '1', value: 'def' }]

Array.union

배열간 합집합 구현.

console.log([1, 2, 3].union([4])); // [1, 2, 3, 4]

Array.intersect

배열간 교집합 구현.

console.log([1, 2, 3].intersect([2])); // [2]

Array.complement

배열간 차집합 구현.

console.log([1, 2, 3].complement([3])); // [1, 2]

Array.isSuperset

입력받은 배열이 현재 배열의 서브셋인지의 여부를 리턴한다.

console.log([1, 2, 3].isSuperset([1])); // true

Date module

Date.format

Date 객체를 지정한 날짜형식의 문자열로 리턴한다.

const date = new Date();
const formatted = date.format('yyyy-MM-dd HH:mm:ss / E', {
  lang: 'ko', // default value ['ko'|'en'|'ch']
  fillZero: true, // default value
});
console.log(formatted); // 2019-01-01 12:34:56 / 화

yyyy: 네 자릿수 연도 yy: 두 자릿수 연도 MM: 두 자릿수 월 MO: 한국어/영어/한자 월(1월,2월,... / January,February,... / 1月,2月,...) dd: 두 자릿수 일 E: 옵션(lang)에 따라 요일명(월,화,... / Mon,Tue,... / 月,火,...) 표기 HH: 두 자릿수 시 hh: 두 자릿수 시 (12시간제) mm: 두 자릿수 분 ss: 두 자릿수 초 ms: 밀리초 ap/AP: 옵션(lang)에 따라 'AM' / '오전' / '午前' 등 표기 $Y: 옵션(lang)에 따라 '년' / '年' 표기 $M: 옵션(lang)에 따라 '월' / '月' 표기 $D: 옵션(lang)에 따라 '일' / '日' 표기 $H: 옵션(lang)에 따라 '시' / '時' 표기 $m: 옵션(lang)에 따라 '분' / '分' 표기 $s: 옵션(lang)에 따라 '초' / '秒' 표기

Date.collect

문자열을 마스킹하여 날짜를 추출하여 Date 타입으로 리턴한다.

millisecond는 추출대상에서 예외.

console.log(new Date().collect(
  'yyyy-MM-dd HH:mm:ss',
  '2019-01-01 13:59:59',
));
// 2019-01-01T13:59:59.000Z

yyyy: 네 자릿수 연도 MM: 두 자릿수 월 dd: 두 자릿수 일 HH: 두 자릿수 시 (24시간제) hh: 두 자릿수 시 (12시간제) mm: 두 자릿수 분 ss: 두 자릿수 초

Date.isToday

해당 Date 타입의 날짜가 오늘 날짜인지 체크한다.

const today = new Date();
console.log(today.isToday()); // true

today.setFullYear(today.getFullYear() - 1);
console.log(today.isToday()); // false

Date.setYesterday

오늘로부터 N일 전으로 날짜를 설정한다.

console.log(new Date().setYesterday(10)); // 오늘부터 10일 전 날짜

Date.setTomorrow

오늘로부터 N일 이후의 날짜를 설정한다.

console.log(new Date().setTomorrow(10)); // 오늘부터 10일 후 날짜

Number module

Number.zf

지정한 숫자만큼 '0'을 해당 숫자 앞에 추가하여 문자열로 리턴한다.

const value = 123;
value.zf(2);
console.log(value); // 00123

Number.toDigits

숫자(소수포함)를 자릿수 구분 문자열로 리턴한다.

const value = 1000.123;
console.log(value.toDigits()); // 1,000.123

Number.inRange

숫자가 범위내의 수인지의 여부에 따라 true/false를 리턴한다. 1개의 파라미터만 주어지면 0부터 해당 값 내 범위인지를 체크한다.

console.log((10).inRange(9, 10)); // true
console.log((10).inRange(10)); // true
console.log((-2).inRange(-1)); // false

Set module

Set.union

세트간 합집합 구현.

const value = new Set([1, 2, 3]);
console.log(value.union([4])); // [1, 2, 3, 4], 배열 인자 받음
console.log(value.union(new Set([4]))); // set: [1, 2, 3, 4], 세트 인자 받음

Set.intersect

세트간 교집합 구현.

const value = new Set([1, 2, 3]);
console.log(value.intersect([2])); // [2], 배열 인자 받음
console.log(value.intersect(new Set([2]))); // [2], 세트 인자 받음

Set.complement

세트간 차집합 구현.

const value = new Set([1, 2, 3]);
console.log(value.complement([3])); // [1, 2], 배열 인자 받음
console.log(value.complement(new Set([3]))); // [1, 2], 세트 인자 받음

Set.isSuperset

입력받은 세트가 현재 세트의 서브셋인지의 여부를 리턴한다.

const value = new Set([1, 2, 3]);
console.log(value.isSuperset([1])); // true, 배열 인자 받음
console.log(value.isSuperset(new Set([1]))); // true, 세트 인자 받음

String module

String.zf

지정한 숫자만큼 '0'을 해당 문자열 앞에 추가한다.

const value = '123';
value.zf(2);
console.log(value); // 00123

String.truthy

빈 문자열이 아닐 경우 true, 그렇지 않으면 false 반환.

const value = '';
console.log(value.truthy()); // false

String.falsy

빈 문자열일 경우 true, 그렇지 않으면 false 반환.

const value = '';
console.log(value.falsy()); // true

String.format

문자열에 포함된 { } 안의 값을 파라미터 오브젝트의 동일 키에 해당하는 값으로 치환한다.

const value = '안녕하세요. {mornya} 입니다.';
const result = value.format({ mornya: '염전노예' });
console.log(result); // 안녕하세요. 염전노예 입니다.

String.toCamelCase

문자열을 camelCase 표기법으로 치환한다.

const value = 'HelloWorld';
console.log(value.toCamelCase()); // helloWorld

String.extractNumber

문자열에서 숫자만 추출해낸다.

const value = '안녕 "M0123" 친구야. 넌 43살이 맞니?';
console.log(value.extractNumber()); // 012343

Change Log

프로젝트 변경사항은 CHANGELOG.md 파일 참조.

License

프로젝트 라이센스는 LICENSE 파일 참조.