Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@yuheiy/childnode-ponyfill

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@yuheiy/childnode-ponyfill - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

2

lib/childnode-ponyfill.d.ts
export declare const before: (childNode: Element | CharacterData | DocumentType, ...nodes: (string | Node)[]) => void;
export declare const after: (childNode: Element | CharacterData | DocumentType, ...nodes: (string | Node)[]) => void;
export declare const replaceWith: (childNode: Element | CharacterData | DocumentType, ...nodes: (string | Node)[]) => void;
export declare const remove: (childNode: Element | CharacterData | DocumentType, ...nodes: (string | Node)[]) => void;
export declare const remove: (childNode: Element | CharacterData | DocumentType) => void;

@@ -10,18 +10,10 @@ "use strict";

};
var makePonyfill = function (methodName, method) {
return function (childNode) {
var nodes = [];
for (var _i = 1; _i < arguments.length; _i++) {
nodes[_i - 1] = arguments[_i];
}
var Implement = find([Element, DocumentType, CharacterData], function (Impl) { return childNode instanceof Impl; });
if (!Implement) {
throw new TypeError('childNode must be Element, DocumentType or CharacterData');
}
if (typeof Implement.prototype[methodName] === 'function') {
return (_a = childNode)[methodName].apply(_a, nodes);
}
return method.apply(void 0, [childNode].concat(nodes));
var _a;
};
var getNativeMethod = function (childNode, methodName) {
var Implement = find([Element, DocumentType, CharacterData], function (Impl) { return childNode instanceof Impl; });
if (!Implement) {
throw new TypeError('childNode must be Element, DocumentType or CharacterData');
}
if (typeof Implement.prototype[methodName] === 'function') {
return childNode[methodName];
}
};

@@ -47,3 +39,3 @@ var createDocumentFragmentFromNodes = function () {

};
exports.before = makePonyfill('before', function (childNode) {
exports.before = function (childNode) {
var nodes = [];

@@ -53,6 +45,10 @@ for (var _i = 1; _i < arguments.length; _i++) {

}
var nativeBefore = getNativeMethod(childNode, 'before');
if (nativeBefore) {
return nativeBefore.apply(void 0, nodes);
}
var frag = createDocumentFragmentFromNodes.apply(void 0, nodes);
childNode.parentNode.insertBefore(frag, childNode);
});
exports.after = makePonyfill('after', function (childNode) {
};
exports.after = function (childNode) {
var nodes = [];

@@ -62,6 +58,10 @@ for (var _i = 1; _i < arguments.length; _i++) {

}
var nativeAfter = getNativeMethod(childNode, 'after');
if (nativeAfter) {
return nativeAfter.apply(void 0, nodes);
}
var frag = createDocumentFragmentFromNodes.apply(void 0, nodes);
childNode.parentNode.insertBefore(frag, childNode.nextSibling);
});
exports.replaceWith = makePonyfill('replaceWith', function (childNode) {
};
exports.replaceWith = function (childNode) {
var nodes = [];

@@ -71,2 +71,6 @@ for (var _i = 1; _i < arguments.length; _i++) {

}
var nativeReplaceWith = getNativeMethod(childNode, 'replaceWith');
if (nativeReplaceWith) {
return nativeReplaceWith.apply(void 0, nodes);
}
if (childNode.parentNode) {

@@ -76,8 +80,12 @@ var frag = createDocumentFragmentFromNodes.apply(void 0, nodes);

}
});
exports.remove = makePonyfill('remove', function (childNode) {
};
exports.remove = function (childNode) {
var nativeRemove = getNativeMethod(childNode, 'remove');
if (nativeRemove) {
return nativeRemove();
}
if (childNode.parentNode) {
childNode.parentNode.removeChild(childNode);
}
});
};
//# sourceMappingURL=childnode-ponyfill.js.map

@@ -14,3 +14,3 @@ {

},
"version": "0.0.1",
"version": "0.0.2",
"main": "lib/childnode-ponyfill.js",

@@ -23,3 +23,3 @@ "types": "lib/childnode-ponyfill.d.ts",

"scripts": {
"test": "npm run build && ava",
"test": "mocha",
"build": "NODE_ENV=production tsc -p .",

@@ -30,14 +30,12 @@ "watch": "tsc -p . --watch",

"devDependencies": {
"ava": "^0.22.0",
"browser-env": "^3.2.0",
"typescript": "^2.5.2"
"@types/mocha": "^2.2.43",
"@types/node": "^8.0.28",
"mocha": "^3.5.3",
"ts-node": "^3.3.0",
"typescript": "^2.5.2",
"window": "^4.2.0"
},
"publishConfig": {
"access": "public"
},
"ava": {
"require": [
"./test/helpers/setup-browser-env.js"
]
}
}

@@ -15,3 +15,3 @@ # ChildNode ponyfill

import {replaceWith} from '@yuheiy/childnode-ponyfill'
const oldElement = document.getElementById('#old')
const oldElement = document.querySelector('#old')
const newElement = document.createElement('div')

@@ -25,18 +25,34 @@ replaceWith(oldElement, newElement)

Same API with [`ChildNode.before()`](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/before)
```typescript
export declare const before: (childNode: Element | CharacterData | DocumentType, ...nodes: (string | Node)[]) => void;
```
Same API with [`ChildNode.before()`](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/before).
### after(childNode, ...nodes)
Same API with [`ChildNode.after()`](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/after)
```typescript
export declare const after: (childNode: Element | CharacterData | DocumentType, ...nodes: (string | Node)[]) => void;
```
Same API with [`ChildNode.after()`](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/after).
### replaceWith(childNode, ...nodes)
Same API with [`ChildNode.replaceWith()`](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/replaceWith)
```typescript
export declare const replaceWith: (childNode: Element | CharacterData | DocumentType, ...nodes: (string | Node)[]) => void;
```
Same API with [`ChildNode.replaceWith()`](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/replaceWith).
### remove(childNode)
Same API with [`ChildNode.remove()`](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove)
```typescript
export declare const remove: (childNode: Element | CharacterData | DocumentType) => void;
```
Same API with [`ChildNode.remove()`](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove).
## License
MIT
type ImplementsOfChildNode = Element | DocumentType | CharacterData
type nodes = Array<string | Node>
type ChildNodeMethodNames = 'before' | 'after' | 'replaceWith' | 'remove'
type Nodes = Array<string | Node>

@@ -12,16 +13,13 @@ const find = <T>(arr: Array<T>, predicate: (item: T, idx: number, arr: Array<T>) => boolean): T | void => {

const makePonyfill = (methodName: string, method: (childNode: ImplementsOfChildNode, ...nodes: nodes) => void) => {
return (childNode: ImplementsOfChildNode, ...nodes: nodes): void => {
const Implement = find([Element, DocumentType, CharacterData], (Impl) => childNode instanceof Impl)
if (!Implement) {
throw new TypeError('childNode must be Element, DocumentType or CharacterData')
}
if (typeof (Implement as any).prototype[methodName] === 'function') {
return (childNode as any)[methodName](...nodes)
}
return method(childNode, ...nodes)
const getNativeMethod = (childNode: ImplementsOfChildNode, methodName: ChildNodeMethodNames): (() => void) | void => {
const Implement = find([Element, DocumentType, CharacterData], (Impl) => childNode instanceof Impl)
if (!Implement) {
throw new TypeError('childNode must be Element, DocumentType or CharacterData')
}
if (typeof (Implement as any).prototype[methodName] === 'function') {
return (childNode as any)[methodName]
}
}
const createDocumentFragmentFromNodes = (...nodes: /*nodes*/Array<any>): DocumentFragment => {
const createDocumentFragmentFromNodes = (...nodes: /*Nodes*/Array<any>): DocumentFragment => {
return nodes.reduce((frag, node) => {

@@ -41,13 +39,28 @@ if (typeof node === 'string') {

export const before = makePonyfill('before', (childNode: ImplementsOfChildNode, ...nodes: nodes) => {
export const before = (childNode: ImplementsOfChildNode, ...nodes: Nodes) => {
const nativeBefore = getNativeMethod(childNode, 'before')
if (nativeBefore) {
return nativeBefore(...nodes)
}
const frag = createDocumentFragmentFromNodes(...nodes)
;(childNode as any).parentNode.insertBefore(frag, childNode)
})
}
export const after = makePonyfill('after', (childNode: ImplementsOfChildNode, ...nodes: nodes) => {
export const after = (childNode: ImplementsOfChildNode, ...nodes: Nodes) => {
const nativeAfter = getNativeMethod(childNode, 'after')
if (nativeAfter) {
return nativeAfter(...nodes)
}
const frag = createDocumentFragmentFromNodes(...nodes)
;(childNode as any).parentNode.insertBefore(frag, childNode.nextSibling)
})
}
export const replaceWith = makePonyfill('replaceWith', (childNode: ImplementsOfChildNode, ...nodes: nodes) => {
export const replaceWith = (childNode: ImplementsOfChildNode, ...nodes: Nodes) => {
const nativeReplaceWith = getNativeMethod(childNode, 'replaceWith')
if (nativeReplaceWith) {
return nativeReplaceWith(...nodes)
}
if (childNode.parentNode) {

@@ -57,8 +70,13 @@ const frag = createDocumentFragmentFromNodes(...nodes)

}
})
}
export const remove = makePonyfill('remove', (childNode: ImplementsOfChildNode) => {
export const remove = (childNode: ImplementsOfChildNode) => {
const nativeRemove = getNativeMethod(childNode, 'remove')
if (nativeRemove) {
return nativeRemove()
}
if (childNode.parentNode) {
childNode.parentNode.removeChild(childNode)
}
})
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc