Skip to content

Commit

Permalink
Update for TypeScript 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmath committed Apr 12, 2017
1 parent e5cefbb commit 5c80353
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 188 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/coverage/
/lib/
/node_modules/
/typings/
/npm-debug.log
/npm-debug.*
4 changes: 1 addition & 3 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/coverage/
/typings/
/src/
/lib/**/*.test.*
/.editorconfig
/.gitignore
/.travis.yml
/custom.d.ts
/mocha.opts
/tsconfig.build.json
/tsconfig.json
/tslint.json
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ language: node_js
node_js:
- "6"
- "4"
- "0.12"
- "0.10"
before_script:
- npm run ci:typings
after_success:
- npm run ci:coveralls
before_deploy:
Expand All @@ -18,7 +14,7 @@ deploy:
secure: E5iBlZDws/vjwNEhQ/qZxKs+7/lbIsK8DOPs4l2nBZuDZL8EBfpSIUMelXPCNTxShsUY4UBrtcFmnHp2gcc4d300/3oG/KJQFOAf0dDkJHVJdYgjKVWxOLX499jlSxYCI+MPvgIU7zglw1seI2K6ahxp7i/ngJVg99mlvLH8LuqG7XMOvA6OttwKZJBpJDt+dC3eRHS6bSv6wahtnpU1RH0I1clcAulxzeRC4z5fs+wKmKAqfPRz4cDdIIFeaQxjGccwDnBCjg3MmdIIYFSXAqnNmSi6knEObe+rFIXm+eoY+Wqo/7JGvwh8Bv1kciK362PzDJgeLR0nFhGE2STVPlJ5Ap57XuSDnBDfLj+DyTtsSDVr/PTjyHj+XFhC5VPUdN0zgAwdw5JLpq63re2mk71gmsVKAlmXDFDm99PS9udaEdjv+tMMiDzTwcMxpMNEjyuOgwXJtS1K6YNRCa3iT2NLAiGTVHHCxXaU85ygzMFmWVCebzpNDZh6OlSRhoxFH5bfubnIjLBtbw0glGjwZ+mCKXlMaikdtMvA4e7NKTvRvmiQeIbxcmkDGFti8xZLDUOzTCYE/huG+o96N+jxElUTvBhCnSoi9rvdSu1pDUFfw9Cy8qANGroU/nCFUVbDRs2RhgC6osd9BMfvUvWNlOeozgx85skngXosbbUyBgU=
skip_cleanup: true
on:
repo: akim-mcmath/deep-map
repo: mcmath/deep-map
node: "4"
tags: true
after_deploy:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016 Akim McMath
Copyright (c) 2016-2017 Akim McMath

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
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,18 @@ let bool: boolean = deepMap<{n: boolean}>({n: 2}, isPositive).n; // :)

## License

Copyright &copy; 2016 Akim McMath. Licensed under the [MIT License][license].
Copyright &copy; 2016&ndash;2017 Akim McMath. Licensed under the [MIT License][license].

[version-badge]: https://img.shields.io/npm/v/deep-map.svg?style=flat-square
[license-badge]: https://img.shields.io/npm/l/deep-map.svg?style=flat-square
[build-badge]: https://img.shields.io/travis/akim-mcmath/deep-map/master.svg?style=flat-square
[coverage-badge]: https://img.shields.io/coveralls/akim-mcmath/deep-map/master.svg?style=flat-square&service=github
[dependencies-badge]: https://img.shields.io/gemnasium/akim-mcmath/deep-map.svg?style=flat-square
[build-badge]: https://img.shields.io/travis/mcmath/deep-map/master.svg?style=flat-square
[coverage-badge]: https://img.shields.io/coveralls/mcmath/deep-map/master.svg?style=flat-square&service=github
[dependencies-badge]: https://img.shields.io/gemnasium/mcmath/deep-map.svg?style=flat-square
[npm]: https://www.npmjs.com/package/deep-map
[license]: LICENSE
[travis]: https://travis-ci.org/akim-mcmath/deep-map
[coveralls]: https://coveralls.io/github/akim-mcmath/deep-map?branch=master
[gemnasium]: https://gemnasium.com/akim-mcmath/deep-map
[deep-map-keys]: https://github.com/akim-mcmath/deep-map-keys
[travis]: https://travis-ci.org/mcmath/deep-map
[coveralls]: https://coveralls.io/github/mcmath/deep-map?branch=master
[gemnasium]: https://gemnasium.com/mcmath/deep-map
[deep-map-keys]: https://github.com/mcmath/deep-map-keys
[typescript]: http://www.typescriptlang.org/
[array-prototype-map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
10 changes: 0 additions & 10 deletions custom.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion mocha.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
src/**/*.ts
--compilers ts:ts-node/register
--recursive
--reporter dot
44 changes: 25 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@
"version": "1.4.2",
"description": "Transforms nested values of complex objects",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"types": "lib/index.d.ts",
"scripts": {
"build:compile": "tsc",
"build:compile": "tsc -p tsconfig.build.json",
"build:remove": "rimraf lib",
"build": "npm run build:remove && npm run build:compile",
"test:lint": "tslint 'src/**/*.ts'",
"test:unit": "istanbul cover -e .ts -x '*.test.ts' _mocha -- src --opts mocha.opts",
"test:lint": "tslint \"src/**/*.ts\"",
"test:unit": "istanbul cover -e .ts -x \"*.test.ts\" _mocha -- --opts mocha.opts",
"test:report": "npm test && open coverage/lcov-report/index.html",
"test": "npm run test:lint && npm run test:unit",
"ci:typings": "typings install",
"ci:coveralls": "cat coverage/lcov.info | coveralls"
},
"engines": {
"node": ">=0.10"
"node": ">=4"
},
"repository": {
"type": "git",
"url": "git+https://github.com/akim-mcmath/deep-map.git"
"url": "git+https://github.com/mcmath/deep-map.git"
},
"keywords": [
"map",
Expand All @@ -38,24 +37,31 @@
"author": "Akim McMath <akim.elijah.mcmath@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/akim-mcmath/deep-map/issues"
"url": "https://github.com/mcmath/deep-map/issues"
},
"homepage": "https://github.com/akim-mcmath/deep-map#readme",
"homepage": "https://github.com/mcmath/deep-map#readme",
"devDependencies": {
"@types/chai": "^3.5.0",
"@types/es6-weak-map": "^1.2.0",
"@types/lodash": "^4.14.62",
"@types/mocha": "^2.2.40",
"@types/sinon": "^2.1.2",
"@types/sinon-chai": "^2.7.27",
"chai": "^3.5.0",
"coveralls": "^2.11.12",
"coveralls": "^2.13.0",
"es6-weak-map": "^2.0.2",
"istanbul": "1.1.0-alpha.1",
"mocha": "^3.0.2",
"rimraf": "^2.5.4",
"sinon": "^1.17.5",
"sinon-chai": "^2.8.0",
"mocha": "^3.2.0",
"rimraf": "^2.6.1",
"sinon": "^2.1.0",
"sinon-chai": "^2.9.0",
"ts-node": "~1.2.3",
"tslint": "^3.14.0",
"typescript": "^1.8.10",
"typings": "^1.3.2"
"tslint": "^5.1.0",
"typescript": "^2.2.2"
},
"dependencies": {
"es6-weak-map": "^2.0.1",
"lodash": "^4.15.0"
"es6-weak-map": "^2.0.2",
"lodash": "^4.17.4",
"tslib": "^1.6.0"
}
}
36 changes: 26 additions & 10 deletions src/deep-map.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
import WeakMap = require('es6-weak-map');
import {isArray, isObject} from 'lodash';
import {isArray, isObject, isFunction, isNil} from 'lodash';

interface NonPrimitive extends Object {
[key: string]: any;
[index: number]: any;
export interface DeepMapModule {
<T>(object: any, mapFn: MapFn, options?: Opts): T;
default<T>(object: any, mapFn: MapFn, options?: Opts): T;
}

export interface MapFn {
(value: any, key: string|number): any;
(value: any, key: string | number): any;
}

export interface Opts {
thisArg?: any;
inPlace?: boolean;
}

export class DeepMap {
export const deepMapModule: DeepMapModule = function deepMap(object: any, mapFn: MapFn, options?: Opts): any {
options = isNil(options) ? {} : options;

private cache = new WeakMap<NonPrimitive, any>();
if (!mapFn) {
throw new Error('mapFn is required');
} else if (!isFunction(mapFn)) {
throw new TypeError('mapFn must be a function');
} else if (!isObject(options)) {
throw new TypeError('options must be an object');
}

return new DeepMap(mapFn, options).map(object);
} as any;

deepMapModule.default = deepMapModule;

class DeepMap {

private cache = new WeakMap<object, any>();

constructor(
private mapFn: MapFn,
private opts: Opts
) { }

public map(value: any, key?: string|number): any {
public map(value: any, key?: string | number): any {
return isArray(value) ? this.mapArray(value) :
isObject(value) ? this.mapObject(value) :
this.mapFn.call(this.opts.thisArg, value, key);
Expand All @@ -46,15 +62,15 @@ export class DeepMap {
return result;
}

private mapObject(obj: NonPrimitive): NonPrimitive {
private mapObject(obj: object): object {
if (this.cache.has(obj)) {
return this.cache.get(obj);
}

let result = this.opts.inPlace ? obj : {};
this.cache.set(obj, result);

for (let key in obj) {
for (let key in obj as any) {
if (obj.hasOwnProperty(key)) {
result[key] = this.map(obj[key], key);
}
Expand Down
15 changes: 11 additions & 4 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import chai = require('chai');
import sinonChai = require('sinon-chai');
import sinon = require('sinon');
import deepMap = require('./');
import deepMap from './';

declare var require: any;

before(() => {
chai.use(sinonChai);
Expand All @@ -19,6 +21,11 @@ describe('deepMap(object, mapFn, [options])', () => {
deepMap.should.be.a('function');
});

it('may be imported as a CommonJS module or ES2015 default import', () => {
require('./').should.equal(deepMap);
require('./').default.should.equal(deepMap);
});

describe('@object: any', () => {

it('transforms simple object', () => {
Expand All @@ -40,11 +47,11 @@ describe('deepMap(object, mapFn, [options])', () => {
});

it('transforms an object with circular references', () => {
let obj = {two: 2, arr: [3, 4], self: null as any, arr2: null as any[]};
let obj = {two: 2, arr: [3, 4], self: null as any, arr2: null as any};
obj.self = obj;
obj.arr2 = obj.arr;

let exp = {two: 4, arr: [9, 16], self: null as any, arr2: null as any[]};
let exp = {two: 4, arr: [9, 16], self: null as any, arr2: null as any};
exp.self = exp;
exp.arr2 = exp.arr;

Expand Down Expand Up @@ -119,7 +126,7 @@ describe('deepMap(object, mapFn, [options])', () => {
});

it('transforms sub-objects/sub-arrays in place', () => {
deepMap(obj, square, {inPlace: true}).arr.should.equal(arr);
(deepMap(obj, square, {inPlace: true}) as any).arr.should.equal(arr);
});

it('defaults to false', () => {
Expand Down
22 changes: 2 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
import {isFunction, isNil, isObject} from 'lodash';
import {DeepMap, MapFn, Opts} from './deep-map';
import { deepMapModule } from './deep-map';

function deepMap<T>(object: T, mapFn: MapFn, options?: Opts): T;
function deepMap<T>(object: any, mapFn: MapFn, options?: Opts): T;

function deepMap(object: any, mapFn: MapFn, options?: Opts): any {
options = isNil(options) ? {} : options;

if (!mapFn) {
throw new Error('mapFn is required');
} else if (!isFunction(mapFn)) {
throw new TypeError('mapFn must be a function');
} else if (!isObject(options)) {
throw new TypeError('options must be an object');
}

return new DeepMap(mapFn, options).map(object);
}

export = deepMap;
export = deepMapModule;
6 changes: 6 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"exclude": [
"**/*.test.ts"
]
}
25 changes: 21 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"target": "ES5",
"module": "CommonJS",
"moduleResolution": "Node",
"rootDir": "src",
"outDir": "lib",
"newLine": "lf",
"declaration": true,
"sourceMap": true,
"forceConsistentCasingInFileNames": true,
"importHelpers": true,
"noEmitHelpers": true,
"noEmitOnError": true,
"noImplicitAny": true,
"rootDir": "src",
"outDir": "lib"
"noUnusedLocals": true,
"noUnusedParameters": true,
"preserveConstEnums": true,
"skipLibCheck": true,
"strictNullChecks": true,
"stripInternal": true,
"suppressImplicitAnyIndexErrors": true
},
"compileOnSave": false,
"include": [
"src/**/*.ts"
],
"exclude": [
"coverage",
"node_modules",
"lib"
]
Expand Down

0 comments on commit 5c80353

Please sign in to comment.